Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Discord;
- using Discord.Commands;
- using Discord.Net.Providers.UDPClient;
- using Discord.Net.Providers.WS4Net;
- using Discord.WebSocket;
- using System;
- using System.Configuration;
- using System.Reflection;
- using System.Runtime.Remoting.Contexts;
- using System.Threading.Tasks;
- namespace discBot
- {
- class Program
- {
- private readonly DiscordSocketClient _client;
- private readonly IDependencyMap _map = new DependencyMap();
- private readonly CommandService _commands = new CommandService();
- /// <summary>
- /// Program entry point
- /// </summary>
- /// <param name="args"></param>
- static void Main(string[] args)
- {
- // Call the Program constructor, followed by the
- // MainAsync method and wait until it finishes (which should be never).
- new Program().MainAsync().GetAwaiter().GetResult();
- }
- /// <summary>
- /// Construct all the thigns
- /// </summary>
- private Program()
- {
- _client = new DiscordSocketClient(new DiscordSocketConfig
- {
- // How much logging do you want to see?
- LogLevel = LogSeverity.Info,
- // If you or another service needs to do anything with messages
- // (eg. checking Reactions), you should probably
- // set the MessageCacheSize here.
- //MessageCacheSize = 50,
- // If your platform doesn't have native websockets,
- // add Discord.Net.Providers.WS4Net from NuGet,
- // add the `using` at the top, and uncomment this line:
- WebSocketProvider = WS4NetProvider.Instance
- });
- }
- /// <summary>
- /// Start up the engines
- /// </summary>
- /// <returns></returns>
- private async Task MainAsync()
- {
- // Subscribe the logging handler.
- _client.Log += Logger;
- // Centralize the logic for commands into a seperate method.
- await InitCommands();
- // Login and connect.
- await _client.LoginAsync(TokenType.Bot, ConfigurationManager.AppSettings["token"]);
- await _client.StartAsync();
- // Wait infinitely so your bot actually stays connected.
- await Task.Delay(-1);
- }
- /// <summary>
- /// Init all commands and handle them
- /// </summary>
- /// <returns></returns>
- private async Task InitCommands()
- {
- // Repeat this for all the service classes
- // and other dependencies that your commands might need.
- //_map.Add(new SomeServiceClass());
- // Either search the program and add all Module classes that can be found:
- await _commands.AddModulesAsync(Assembly.GetEntryAssembly());
- // Or add Modules manually if you prefer to be a little more explicit:
- //await _commands.AddModuleAsync<PublicModule>();
- // Subscribe a handler to see if a message invokes a command.
- _client.MessageReceived += HandleCommandAsync;
- }
- /// <summary>
- /// Handle commands if they match the expected prfix
- /// </summary>
- /// <param name="arg"></param>
- /// <returns></returns>
- 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(ConfigurationManager.AppSettings["messagePrefix"][0], ref pos) /* || msg.HasMentionPrefix(msg.Discord.CurrentUser, ref pos) */)
- {
- // Create a Command Context
- var context = new CommandContext(msg.Discord, msg);
- // Execute the command. (result does not indicate a return value,
- // rather an object stating if the command executed succesfully).
- IResult result = await _commands.ExecuteAsync(context, pos, _map);
- // 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);
- }
- }
- /// <summary>
- /// Create a named logging handler, so it can be re-used by addons
- /// that ask for a Func<LogMessage, Task>.
- /// </summary>
- /// <param name="message"></param>
- /// <returns></returns>
- private static Task Logger(LogMessage message)
- {
- var cc = Console.ForegroundColor;
- switch (message.Severity)
- {
- case LogSeverity.Critical:
- case LogSeverity.Error:
- Console.ForegroundColor = ConsoleColor.Red;
- break;
- case LogSeverity.Warning:
- Console.ForegroundColor = ConsoleColor.Yellow;
- break;
- case LogSeverity.Info:
- Console.ForegroundColor = ConsoleColor.White;
- break;
- case LogSeverity.Verbose:
- case LogSeverity.Debug:
- Console.ForegroundColor = ConsoleColor.DarkGray;
- break;
- }
- Console.WriteLine($"{DateTime.Now,-19} [{message.Severity,8}] {message.Source}: {message.Message}");
- Console.ForegroundColor = cc;
- return Task.CompletedTask;
- }
- }
- }
- using Discord.Commands;
- using Discord.WebSocket;
- using System.Reflection;
- using System.Threading.Tasks;
- namespace discBot
- {
- public class PublicModule : ModuleBase
- {
- /// <summary>
- /// Say what
- /// </summary>
- /// <param name="echo"></param>
- /// <returns></returns>
- [Command("say"), Summary("Echos a message.")]
- public async Task Say([Remainder, Summary("The text to echo")] string echo)
- {
- // ReplyAsync is a method on ModuleBase
- await ReplyAsync(echo);
- }
- /// <summary>
- /// Super gay
- /// </summary>
- /// <param name="echo"></param>
- /// <returns></returns>
- [Command("isgingergay"), Summary("Echos a message.")]
- public async Task WhatAreYou([Remainder, Summary("The text to echo")] string echo)
- {
- // ReplyAsync is a method on ModuleBase
- await ReplyAsync(echo);
- }
- /// <summary>
- /// Invite
- /// </summary>
- /// <returns></returns>
- [Command("invite")]
- [Summary("Returns the OAuth2 Invite URL of the bot")]
- public async Task Invite()
- {
- var application = await Context.Client.GetApplicationInfoAsync();
- await ReplyAsync(
- $"A user with `MANAGE_SERVER` can invite me to your server here: <https://discordapp.com/oauth2/authorize?client_id={application.Id}&scope=bot>");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement