모든 명령을 자신의 .cs 파일에서 자신의 클래스에 넣으려고합니다. 시작과 명령이 같은 파일에 있기 때문에 파일을 혼란스럽게 만듭니다. 대부분의 다른 사람들이 처음부터 이걸 가지고있는 것처럼 보이지만 나는 그들 중 하나가 아니 었습니다. 여기에 내가 지금 가지고있는 내용은 다음과 같습니다Discord.NET 1.0 새로운 .cs 파일에 명령을 넣는 방법
시작 :
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Diagnostics;
using Discord;
using Discord.WebSocket;
using Discord.Commands;
using Microsoft.Extensions.DependencyInjection;
namespace LockBot__1._0_
{
public class Program
{
private CommandService _commands;
private DiscordSocketClient _client;
private IServiceProvider _services;
public static void Main(string[] args)
=> new Program().StartAsync().GetAwaiter().GetResult();
public async Task StartAsync()
{
_client = new DiscordSocketClient();
_commands = new CommandService();
_services = new ServiceCollection()
.AddSingleton(_client)
.AddSingleton(_commands)
.BuildServiceProvider();
await InstallCommandsAsync();
string token = "SomeToken";
await _client.LoginAsync(TokenType.Bot, token);
await _client.StartAsync();
await Task.Delay(-1);
}
public async Task InstallCommandsAsync()
{
_client.Ready += SetGamePlayAsync;
_client.MessageReceived += HandleCommandAsync;
await _commands.AddModulesAsync(Assembly.GetEntryAssembly());
}
private async Task HandleCommandAsync(SocketMessage messageParam)
{
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);
}
public async Task SetGamePlayAsync()
{
await _client.SetGameAsync("locksteel.me | ~help");
}
}
명령 (단축) :
[Name("Miscellaneous")]
public class Misc : ModuleBase<SocketCommandContext>
{
Random rand = new Random();
[Command("flip")]
[Name("flip")]
[Summary("Flips a coin.")]
[Alias("coin", "coinflip")]
public async Task FlipAsync()
{
string flipToSend;
int flip = rand.Next(1, 3);
if ((flip == 1) && !(flip == 2))
{
flipToSend = "Heads.";
}
else if ((flip == 2) && !(flip == 1))
{
flipToSend = "Tails.";
}
else
{
Debug.WriteLine("~flip error: error establishing random number");
return;
}
await Context.Channel.SendMessageAsync(flipToSend);
Debug.WriteLine("~flip executed successfully in " + Context.Guild.Name);
}
}
참고 : 난 아직도 불화 봇 결정에도있는 moreso 코딩에 상당히 새로운, 그래서 그것을 명심하십시오. 근거는 다음 경우 활성화 내 명령 파일의 다음
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Reflection;
using System.Threading.Tasks;
namespace Discordbot_Techxas
{
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()
{
_client = new DiscordSocketClient();
_commands = new CommandService();
_services = new ServiceCollection()
.AddSingleton(_client)
.AddSingleton(_commands)
.BuildServiceProvider();
//You need to add the Token for your Discord Bot to the code below.
string botToken = "YOUR CODE HERE";
//event subscriptions
_client.Log += Log;
_client.UserJoined += AnnounceUserJoined;
await RegisterCommandsAsync();
await _client.LoginAsync(TokenType.Bot, botToken);
await _client.StartAsync();
await Task.Delay(-1);
}
private async Task AnnounceUserJoined(SocketGuildUser user)
{
var guild = user.Guild;
var channel = guild.DefaultChannel;
await channel.SendMessageAsync($"Welcome, {user.Mention}");
}
private Task Log(LogMessage arg)
{
Console.WriteLine(arg);
return Task.CompletedTask;
}
public async Task RegisterCommandsAsync()
{
_client.MessageReceived += HandleCommandAsync;
await _commands.AddModulesAsync(Assembly.GetEntryAssembly());
}
private async Task HandleCommandAsync(SocketMessage arg)
{
var message = arg as SocketUserMessage;
if (message is null || message.Author.IsBot) return;
int argPos = 0;
if (message.HasStringPrefix("!", ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))
{
var context = new SocketCommandContext(_client, message);
var result = await _commands.ExecuteAsync(context, argPos, _services);
if (!result.IsSuccess)
Console.WriteLine(result.ErrorReason);
}
}
}
}
각 : 내 program.cs에서