i tried this code which i've copied from the github page of discord.net this one works perfectly fine:
using Discord;
using Discord.WebSocket;
using System;
using System.Threading.Tasks;
namespace Adivisor
{
public class Program
{
private DiscordSocketClient _client;
public static void Main(string[] args) => new Program().MainAsync().GetAwaiter().GetResult();
public async Task MainAsync()
{
_client = new DiscordSocketClient();
_client.Log += Log;
_client.MessageReceived += MessageReceived;
string token = ":P";
await _client.LoginAsync(TokenType.Bot, token);
await _client.StartAsync();
await Task.Delay(-1);
}
private async Task MessageReceived(SocketMessage message)
{
if (message.Content == "!hi")
{
await message.Channel.SendMessageAsync("Hello!");
}
}
private Task Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
return Task.CompletedTask;
}
}
}
after i made a little editing on MessageReceived now it looks like this:
private async Task MessageReceived(SocketMessage message)
{
await message.Channel.SendMessageAsync("Hello!");
}
pratically i just removed the if, but there is a huge differnce, seems like the without the if MessageReceived gets called forever...
Like what Xiaoy312 stated, whenever your bot send a message, it also receives it's own message.
(The same also applies for your own user account! Try doing an
@everyoneping in chat and you will see it being highlighted even though it was you who sent it.)You can make your bot ignore it other bots messages by doing this:
Or you can make it check if the message's author's ID equals to the bot's ID. You can check the documentation here.