2017-09-04 139 views
0
using Discord.Commands; 
using Discord; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Runtime.Remoting.Contexts; 
using System.ServiceModel.Channels; 

namespace Leaf 
{ 
    class Leaf 
    { 
     [Command("!!Gpurge")] 
     [RequireBotPermission(Discord.GuildPermission.ManageMessages)] 
     [RequireUserPermission(Discord.GuildPermission.ManageMessages)] 
     [Alias("Clear", "delete")] 
     public async Task Purge(IUserMessage msg, int num = 100) 
     { 
     var purgeMessage = await msg.Channel.SendMessageAsync("!!Gpurge"); 
      var lastMessageID = purgeMessage.Id; 
     if (num <= 100) 
     { 
      var messageToDelete = await msg.Channel.GetMessagesAsync(lastMessageID, Direction.Before, 15).OfType<IUserMessage>().ToList(); 
      await purgeMessage.DeleteAsync(); 
     } 
    } 

**我更改爲一個前綴使用!! G與purge是一個命令,刪除1-100範圍內的許多消息,但很遺憾,它不讀取也不響應在不和諧的應用程序Discord bot C#不執行/讀命令

回答

0

對於Discord.NET,檢查消息是否通過前綴命令應已由異步處理程序處理。

如果你按照文檔/源代碼的例子,你可以搜索文件中某處的HandleCommandAsync函數。

從文檔的例子

基本上,它看起來像這樣:

private async Task HandleCommandAsync(SocketMessage arg) 
    { 
     // Bail out if it's a System Message. 
     var msg = arg as SocketUserMessage; 
     if (msg == null) return; 

     // Create a number to track where the prefix ends and the command begins 
     int pos = 0; 
     // Replace the '!' with whatever character 
     // you want to prefix your commands with. 
     // Uncomment the second half if you also want 
     // commands to be invoked by mentioning the bot instead. 
     if (msg.HasCharPrefix('!', ref pos) /* || msg.HasMentionPrefix(_client.CurrentUser, ref pos) */) 
     { 
      // Create a Command Context. 
      var context = new SocketCommandContext(_client, msg); 

      // Execute the command. (result does not indicate a return value, 
      // rather an object stating if the command executed succesfully). 
      var result = await _commands.ExecuteAsync(context, pos, _services); 

      // Uncomment the following lines if you want the bot 
      // to send a message if it failed (not advised for most situations). 
      //if (!result.IsSuccess && result.Error != CommandError.UnknownCommand) 
      // await msg.Channel.SendMessageAsync(result.ErrorReason); 
     } 
    } 

通知部分msg.HasCharPrefix('!', ref pos)
基本上,它會在嘗試執行命令之前檢查消息是否包含所需的前綴。如果沒有,那麼它什麼也不做。
(請注意,msg.HasStringPrefix()也存在!)

+0

Wen Qin,我插入了代碼,並將行縮小爲3行錯誤,但在這些錯誤中,我不知道爲什麼它不會識別_services _commands和_client – Xpresnvdy

+0

代碼是從文檔的示例中提取的。 _client是你聲明的'DiscordSocketClient',_commands是你聲明的'CommandService',最後_services是你的'IServiceProvider'。你可以在這裏查看文檔的教程:https://discord.foxbot.me/docs/guides/getting_started/intro.html – WQYeo