Skip to content

Instantly share code, notes, and snippets.

@DeltaDizzy
Created December 9, 2017 22:45
Show Gist options
  • Save DeltaDizzy/804a5a989f76f934283ba5616166000c to your computer and use it in GitHub Desktop.
Save DeltaDizzy/804a5a989f76f934283ba5616166000c to your computer and use it in GitHub Desktop.
help me
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Reflection;
using System.Threading.Tasks;
namespace DeltaBot
{
public class CommandHandler
{
private DiscordSocketClient _client;
private CommandService _commands;
private IServiceProvider _services;
string token = "no hackerz allowed";
public CommandHandler(DiscordSocketClient client)
{
}
public async Task StartAsync()
{
_client = new DiscordSocketClient();
_commands = new CommandService();
_services = new ServiceCollection()
.AddSingleton(_client)
.AddSingleton(_commands)
.BuildServiceProvider();
await InstallCommandsAsync();
}
public async Task InstallCommandsAsync()
{
_client.MessageReceived += HandleCommandAsync;
await _commands.AddModulesAsync(Assembly.GetEntryAssembly());
}
private async Task HandleCommandAsync()
{
var message = messageParam as SocketUserMessage;
if (message == null) return;
int argPos = 0;
if (!(message.HasCharPrefix('&', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return;
var context = new SocketCommandContext(_client, message);
var result = await _commands.ExecuteAsync(context, argPos, _services);
if (!result.IsSuccess)
await context.Channel.SendMessageAsync(result.ErrorReason);
}
}
}
Copy link

ghost commented Feb 29, 2020

Hey, in her _services = new ServiceCollection();
Am I right ?

@DeltaDizzy
Copy link
Author

what

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment