So I've been racking my brain trying to figure out how to implement a "talk" function in my game. I'm new to C# programming but I've been doing as much reading and experimenting as I can with the language.
This is what I have so far:
Comm comm = new Comm();
string message = null;
if (InputBox.Text == "say " + message)
{
OutputBox.AppendText(comm.do_say(message));
}
class Comm
{
public string do_say(string message)
{
return "You say: " + message + "\n";
}
}
Now, this doesn't work. I think I know why, but I can't seem to figure out just how to redo it so it does work... I've tried to replace:
(InputBox.Text == "say " + message)
with
(InputBox.Text == "say {0}", message)
and it doesn't work either. So, now I'm out of ideas on how to make this work. I tried searching stackoverflow and google for answers but came up with nothing.
Any help or hints on how to fix it would be great!
Thanks.
You don't know what the message is in advance, right? You need to search for the "Say ", and take the rest of the string as input.
if(InputBox.Text.StartsWith("Say "))
OutputBox.Text += InputBox.Text.SubString(4);
SubString(4) will return whatever's after the first 4 characters in the string, everything after the "Say "
You seem to be looking for pattern matching here, but C# doesn't support pattern matching. In other words, simply writing
if (InputBox.Text == "say " + message)
does not automatically assign "foo" to message whenever the user types "say foo".
Instead, you should probably use regular expressions, which are implemented in C# with the Regex class. Try something like
Match m = Regex.Match(InputBox.Text, #"^say\s+(.*)$", RegexOptions.IgnoreCase);
if (m.Success)
{
OutputBox.AppendText(Comm.GetScreenoutput(m.Groups[1].Value));
}
You don't need to make do_say an instance method, so in the code above I have assumed that Comm is transformed to
static class Comm
{
public static string GetScreenOutput(string message)
{
return "You say: " + message + "\n";
}
}
This code follows the naming conventions for C# code, using Pascal case for method names.
if (InputBox.Text.ToUpper().StartsWith("SAY "))
{
OutputBox.AppendText(comm.do_say(message));
}
This will check if the user used the word say, regardless of the case used.
Related
I am pretty new to making discord bots and have tried to make a filter to when my bot detects a certain word in a message it will delete it. It is throwing no errors, it just will not delete the message. I might just have the code in the wrong spot, but I do not know. If anyone can help me out, I'd appreciate it.
private async Task BadWordsWarn(SocketMessage message)
{
string[] badWords = File.ReadAllLines(Environment.CurrentDirectory + "/wordstodelete.txt");
if (badWords.Any(message.Content.Contains))
await message.DeleteAsync();
}
How are you calling BadWordsWarn? I suspect that you perhaps are not awaiting the async call, or badWords isn't actually being populated for the version of code you're running e.g. is the file being copied to the output directory if you're running the release version?
The C# code works - not optimal as the badWords array should be passed in as opposed to being read every time but there's nothing wrong with the syntax.
Perhaps the Discord message contents is in JSON and needs to be parsed?
If it's a string, this code is case-insensitive & definitely works.
var badWords = new [] {
"grape",
"apple"
};
var messageContent = "good good good grape";
foreach(var word in badWords) {
var contentHasBadWord = messageContent.IndexOf(word, StringComparison.OrdinalIgnoreCase) >= 0;
if (contentHasBadWord) {
Console.WriteLine("Bad word found - " + word);
// delete message
break;
}
}
dotnetfiddle
I'm attempting to read a text file and delete a user entered string. I cannot get it to report a message if the string does not exist.
I cannot explain everything I've tried to this point, it's been many things. I know there is nothing in it's current form that would give me the results I expect, but I've tried many many things and this is currently where it's at. For the code that is there, it's doing everything I'm telling it to do.
if (rButtonDelete.Checked)
{
bool isValid = txtID.Text.Length < 5;
if (txtID.Text == "")
{
lbOne.Items.Add("You must enter a fixture to delete.");
}
else
if(!isValid==false)
{
lbOne.Items.Add("Enter full fixture ID to delete.");
}
else
{
var oldLines = System.IO.File.ReadAllLines(#"F:\09 Quality\CMM Fixtures\fixtures.txt");
var newLines = oldLines.Where(lines => !lines.Contains(txtID.Text));
System.IO.File.WriteAllLines(#"F:\09 Quality\CMM Fixtures\fixtures.txt", newLines);
lbOne.Items.Add(txtID.Text + " was deleted.");
}
}
As stated above, as it exists now, it does everything I am telling it to do. I just need to report that a string being searched for does not exist if in doesn't. No matter what I type into the text box, it tells me it's been deleted, even if it doesn't exist.
How about this:
if (oldLines.Count() == newLines.Count())
{
lbOne.Items.Add(txtID.Text + " does not exist.");
}
else
{
lbOne.Items.Add(txtID.Text + " was deleted.");
}
I want to ask you guys if it is possible to do the following:
Type in textbox something like this "search pluto"
and then it must search for that last word.
This is how I did it but it doens't work because when I do that
my browser opens up twice.
One with "https://www.google.be/#q="
and the other tab that opens is the word that I wrote in
the textbox. Can somebody help me out of this please?
This is the code for this:
string url = "https://www.google.be/#
if (inputTBX.Text.Contains("search ") == true)
{
inputTBX.Text.Replace("search ", "");
string URL = url += inputTBX.Text;
Process.Start(#"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
URL);
inputTBX.Clear();
}
just as a test in a simple console app I tried the following and it worked launching my default browser
var t = "pluto";
Process.Start("http://google.com/search?q=" + t);
This also works
var t = "pluto";
Process.Start("https://www.google.be/search?q=" + t);
in your case you need to get your query string to be the following
https://www.google.be/#q=pluto
your first problem is that you are trying to use the Replace method but you need to assign it into something here is a working solution of your code just tested notice the differences in what I have done
inputTBX.Test = "search pluto";
string url = "https://www.google.be/search?q=";
if (inputTBX.Contains("search "))
{
inputTBX.Text = inputTBX.Replace("search ", "");
string URL = url += inputTBX;
Process.Start(URL); // this will launch your default web browser
inputTBX.Clear();
}
since your query string has the word search in it.. you really don't need this line if (inputTBX.Contains("search ")) but if you keep it it will work with if you pass search planet pluto for example in your textbox
This part
inputTBX.Text.Replace("search ", "");
Is seriously bad, because it will fail to do its job if you have input string like this
"search research on Beethoven's work"
If you want the key phrase to be "search ", you should do this instead
inputTBX.Text.Substring(("search ").Length); //this way it will skip the "search " phrase and use the rests of the phrase for searching
As for your process with the given URL, simply do
string url = "https://www.google.be/#q="; //notice the q= is missing in your code shown
Process.Start(url + inputTBX.Text.Substring(("search ").Length));
This is a follow on from my last question.
The string I am trying to compare comes through as follows:
Discovered Peripherial: <CBPeripheral: 0x39b8b50 identifier = DEEE65FB-FF1F-A6A9-4C3C-5784F41B0D39, Name = "rawr", state = connecting>
What I'm trying to do is check the identifier number to that which I'm storing in my program. To do that, I have done the following:
private void AppendString(string message)
{
message.Substring(message.Length-77, message.Length);
outputContent.text += "\n" + message;
}
The \n is in there because I'm reading in 6 different devices and they all generate the above line. So I needed something to make it easier to read.
Then in my update function I am checking to see if they are similar like so:
if(String.Equals(myValues["UUID"], outputContent.text, StringComparison.OrdinalIgnoreCase))
{
Instantiate(model1, new Vector3(-2.5f, 3.0f,0), Quaternion.identity);
}
However when I run this on my iPad, xCode generates the following message:
ArgumentOutOfRangeException: startIndex + length > this.length
Parameter name: length
Which I'm guessing means I have miscounted the amount of characters I need to count back from in the substring.
My question is this:
Is there a better way in which I can compare my stored value with that of a specific part of a really long string or have I done something silly in my code which is generating that error?
This is a Unity project which I'm building onto an iPad and makes use of some functionality I can't replicate on a mac.
in your code i observed that you are calling Substring() method but not getting/saving its result into some variable .
message.Substring(message.Length-77, message.Length);
as string is an immutable which can not be modified.
hence when you call the Substring() method it cuts the required text and returns, which you have to save it on some variable to proceed further.
as below:
replace your function AppendString() as below :
private void AppendString(string message)
{
message= message.Substring(message.Length-77, message.Length);
outputContent.text += "\n" + message;
}
I'm not sure how to actually block or remove a user using 'SKYPE4COMlib' extention for Visual C#. I can change my status, return messages but I do not know how to do that.
If anyone has an idea of any other useful commands please list them here also.
User LiamaCloud is correct.
There's a IsBlocked property in User interface.
You can do something like this:
ISkype skype = _skype;
var tbb = skype.Friends.Cast<User>().Where(u => u.FullName.Contains("xxx");
foreach(User notAFriend in tbb)
{
notAFriend.IsBlocked = true;
MessageBox.Show(friend.FullName + " " + friend.IsBlocked);
}
I've tested it and it works.
Best