So I wanted to make a discord bot that would pick a random character in game for me and my friends while we are on discord and I just can't make it work. When I run the project in visual studio there are no errors, it says connected. The bot goes online, but when I try to use any command (ping) it does nothing
This is my main script for the
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
namespace GaryBot
{
internal class Program
{
static void Main(string[] args) => new Program().RunBotAsync().GetAwaiter().GetResult();
private DiscordSocketClient _client;
private CommandService _commands;
private IServiceProvider _services;
public async Task RunBotAsync()
{
var configuracoes = new DiscordSocketConfig()
{
GatewayIntents = GatewayIntents.Guilds | GatewayIntents.GuildMessages
};
_client = new DiscordSocketClient(configuracoes);
_commands = new CommandService();
_services = new ServiceCollection()
.AddSingleton(_client)
.AddSingleton(_commands)
.BuildServiceProvider();
string token = "XXX";
_client.Log += _client_Log;
await RegisterCommandsAsync();
await _client.LoginAsync(TokenType.Bot, token);
await _client.StartAsync();
await Task.Delay(-1);
}
private Task _client_Log(LogMessage arg)
{
Console.WriteLine(arg);
return Task.CompletedTask;
}
public async Task RegisterCommandsAsync()
{
_client.MessageReceived += HandleCommandAsync;
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
}
private async Task HandleCommandAsync(SocketMessage arg)
{
var message = arg as SocketUserMessage;
var context = new SocketCommandContext(_client, message);
if (message.Author.IsBot) return;
int argPos = 0;
if(message.HasStringPrefix("]", ref argPos))
{
var result = await _commands.ExecuteAsync(context, argPos, _services);
if (!result.IsSuccess) Console.WriteLine(result.ErrorReason);
}
}
}
}
And this is my Commands script
using Discord.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GaryBot.Modules
{
public class Commands : ModuleBase<SocketCommandContext>
{
[Command("ping")]
public async Task Ping()
{
await ReplyAsync("Pong");
}
}
}
I searched multiple threads and nothing worked for me