C
C#2y ago
2fur

Do something when event fired

Hello Im kinda new with programming stuff etc and i would like to ask how can I call a function by event or do something when its fired cause actually my event skill are very low. If it matters, i'm trying to create a discord bta using dsharpplus, where there are events that i have trouble handling. currently the code I have is the one below, but this one gives me an error: Severity Code Description Project File Row Skip status Error CS1643 Not all paths in code return value in lambda expression of type "AsyncEventHandler<DiscordClient, MessageCreateEventArgs>" MeowBot D:\Vs projects\MeowBot\Program.cs 168 Active and its doesnt give me any log in sonsole
public static int newMessages;
public static void NewMessageFunction<TEventArgs>(AsyncEventHandler<DiscordClient, MessageCreateEventArgs> MessageCreated)
{
MessageCreated += (sender, e) =>
{
Console.WriteLine("+1 message, messages: " + newMessages);
newMessages++;


};
}
public static int newMessages;
public static void NewMessageFunction<TEventArgs>(AsyncEventHandler<DiscordClient, MessageCreateEventArgs> MessageCreated)
{
MessageCreated += (sender, e) =>
{
Console.WriteLine("+1 message, messages: " + newMessages);
newMessages++;


};
}
3 Replies
TheBoxyBear
TheBoxyBear2y ago
AsyncEvent += async (sender, e) => {}
AsyncEvent += async (sender, e) => {}
But consider handling it either in a delegate variable or method so you can unsubscribe from the event. Using an anonymous function like this, you lose control over the handler function after subscribing to the event so you cannot reference it later to unsubscribe.
2fur
2furOP2y ago
Thx bro
Want results from more Discord servers?
Add your server