I have a problem. I started working with TwitchLib to play some API and I have a problem with Events. Here I have the code from my class "ChatBot" where there is the whole main code for connecting to my Twitch account and receiving, for example, Chat messages. And the messages are displayed in the "ChatBot" class as much as possible, but when I created another public Event to be able to receive these messages along with the rest of the data (and it is full of them) then in the place "MessageReceivedEvent (sender, e);" I get the message that "MessageReceivedEvent (sender, e);" is Null. I would like to ask you what am I doing wrong? After all, data is assigned, so MessageReceivedEvent should not be empty.
public event EventHandler <OnMessageReceivedArgs> MessageReceivedEvent; <- this is at the top of the code
public void MessageReceived (object sender, OnMessageReceivedArgs e) <- This is an Event with TwitchLib
{
try
{
//Here I get the message that the code below is Null - This is an Event created by me
MessageReceivedEvent (sender, e);
}
catch (Exception ex)
{
Console.WriteLine ("[MessageReceivedEvent]" + ex.Message);
}
// This code below works
if (e.ChatMessage.Message.StartsWith ("hi", StringComparison.InvariantCultureIgnoreCase))
{
client.SendMessage (TwitchInfo.ChannelName, "Hi!" + e.ChatMessage.DisplayName);
}
}
In UserControl where I want to receive messages from ChatBot I have:
ChatBot bot = new ChatBot();
private void Chat_Load (object sender, EventArgs e)
{
bot.MessageReceivedEvent + = Bot_MessageReceivedEvent;
}
private void Bot_MessageReceivedEvent (object sender, TwitchLib.Client.Events.OnMessageReceivedArgs e)
{
Console.WriteLine (e.ChatMessage.Message);
}
Is there any way to embrace this Event? I saw that StackOverflow has a lot of topics with this problem but I can't use any solution at my code.