C#でBOTを作りたくこちらのサイトを拝見して試しにつくってみたのですがうまく動いてませんなんとか動かしたいです
Program.cs
C#
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using Discord.Commands; 7using Discord.WebSocket; 8using Microsoft.Extensions.DependencyInjection; 9using System.Reflection; 10using Discord; 11namespace bot 12{ 13 class Program 14 { 15 public static DiscordSocketClient client; 16 public static CommandService commands; 17 public static IServiceProvider services; 18 static void Main(string[] args) => new Program().MainAsync().GetAwaiter().GetResult(); 19 public async Task MainAsync() 20 { 21 client = new DiscordSocketClient(); 22 commands = new CommandService(); 23 services = new ServiceCollection().BuildServiceProvider(); 24 client.MessageReceived += CommandRecieved; 25 client.Log += Log; 26 string token = "NjczNDM2MTM0ODYwMTI4MjU2.XkJpYQ.ZkczJ6aqjBthTiintQS4DfQoRt8"; 27 await commands.AddModulesAsync(Assembly.GetEntryAssembly(),services); 28 await client.LoginAsync(TokenType.Bot, token); 29 await client.StartAsync(); 30 await Task.Delay(-1); 31 } 32 /// <summary> 33 /// </summary> 34 /// <param name="msgParam"></param> 35 /// <returns></returns> 36 private async Task CommandRecieved(SocketMessage messageParam) 37 { 38 var message = messageParam as SocketUserMessage; 39 Console.WriteLine("{0} {1}:{2}", message.Channel.Name, message.Author.Username, message); 40 if (message == null) { return; } 41 if (message.Author.IsBot) { return; } 42 int argPos = 0; 43 if (!(message.HasCharPrefix('.', ref argPos) || message.HasMentionPrefix(client.CurrentUser, ref argPos))) { return; } 44 var context = new CommandContext(client, message); 45 var result = await commands.ExecuteAsync(context, argPos, services); 46 if (!result.IsSuccess) { await context.Channel.SendMessageAsync(result.ErrorReason); } 47 } 48 /// <summary> 49 /// コンソール表示処理 50 /// </summary> 51 /// <param name="msg"></param> 52 /// <returns></returns> 53 private Task Log(LogMessage msg) 54 { 55 Console.WriteLine(msg.ToString()); 56 return Task.CompletedTask; 57 } 58 } 59}
Messages.cs
C#
1using Discord.Commands; 2using System; 3using System.Collections.Generic; 4using System.Linq; 5using System.Text; 6using System.Threading.Tasks; 7namespace bot 8{ 9 public class Messages : ModuleBase 10 { 11 private readonly Dictionary<int, string> rule = new Dictionary<int, string>() 12 { 13 {0, "ナワバリ"}, 14 {1, "エリア"}, 15 {2, "ホコ"}, 16 {3, "ヤグラ"}, 17 {4, "アサリ"}, 18 }; 19 private readonly Dictionary<int, string> stage = new Dictionary<int, string>() 20 { 21 {0, "バッテラストリート" }, 22 {1 , "フジツボスポーツクラブ"}, 23 {2 , "ガンガゼ野外音楽堂"}, 24 {3 , "チョウザメ造船"}, 25 {4 , "海女美術大学"}, 26 {5 , "コンブトラック"}, 27 {6 , "マンタマリア号"}, 28 {7 , "ホッケふ頭"}, 29 {8 , "タチウオパーキング"}, 30 {9 , "エンガワ河川敷"}, 31 {10, "モズク農園"}, 32 {11, "Bバスパーク"}, 33 {12, "デボン海洋博物館"}, 34 {13, "ザトウマーケット"}, 35 {14, "ハコフグ倉庫"}, 36 {15, "アロワナモール"} 37 }; 38 [Command("Rl")] 39 public async Task Rl() 40 { 41 Random random = new Random(); 42 int randomRule = random.Next(5); 43 int randomStage = random.Next(16); 44 string Messages = "次の試合のルールは、\n ・**" + rule[randomRule].ToString() + "**\n\n" + "次の試合のステージは、\n ・**" + stage[randomStage].ToString() + "**\n"; 45 await ReplyAsync(Messages); 46 } 47 } 48}
エラー
今のところない
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/02/12 01:25