我一直在試圖從0.9.6 Discord.NET API交換了我的代碼到新的1.0.1 API,它的基本要求一個完整的重組我碼。但是我首先遇到了一些麻煩,實際上讓機器人啓動並運行。不和諧機器人運行,但將無法連接到服務器
我設置代碼機構按指南鏈接here
雖然它運行沒有錯誤,機器人本身是不是在網上我的服務器出現。
你提問之前,我實際上已經取代了「博特令牌這裏」與實際機器人令牌。
namespace DiscordBot{
public class Program
{
private CommandService commands;
private DiscordSocketClient client;
private IServiceProvider services;
static void Main(string[] args) => new Program().Start().GetAwaiter().GetResult();
public async Task Start()
{
client = new DiscordSocketClient();
commands = new CommandService();
string token = "<token>";
services = new ServiceCollection()
.BuildServiceProvider();
await InstallCommands();
await client.LoginAsync(TokenType.Bot, token);
await client.StartAsync();
await Task.Delay(-1);
}
public async Task InstallCommands()
{
// Hook the MessageReceived Event into our Command Handler
client.MessageReceived += HandleCommand;
// Discover all of the commands in this assembly and load them.
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
}
public async Task HandleCommand(SocketMessage messageParam)
{
// Don't process the command if it was a System Message
var message = messageParam as SocketUserMessage;
if (message == null) return;
// Create a number to track where the prefix ends and the command begins
int argPos = 0;
// Determine if the message is a command, based on if it starts with '!' or a mention prefix
if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(client.CurrentUser, ref argPos))) return;
// Create a Command Context
var context = new CommandContext(client, message);
// Execute the command. (result does not indicate a return value,
// rather an object stating if the command executed successfully)
var result = await commands.ExecuteAsync(context, argPos, services);
if (!result.IsSuccess)
await context.Channel.SendMessageAsync(result.ErrorReason);
}
}
}
再來說MyBot.cs類
namespace DiscordBot
{
class MyBot : ModuleBase
{
private CommandService _service;
public MyBot(CommandService service)
{
_service = service;
}
}
}
感謝您的輸入,結果發生了一些事情。 | | System.PlatformNotSupportedException:此平臺不支持Websocket協議 經過一番研究,事實證明它與Windows 7不兼容?你知道有沒有解決這個問題的方法? – John
@john是的,有。但是,這將是一個長期的評論,並會值得一個新的問題 –
好吧,謝謝你讓我知道。我會發佈一個新的問題,然後:) – John