public static void RegisterFriendlyConverters(this CommandsNextExtension commands) { commands.RegisterConverter(new FriendlyDiscordUserConverter()); commands.RegisterConverter(new FriendlyDiscordMemberConverter()); commands.RegisterConverter(new FriendlyDiscordChannelConverter()); commands.RegisterConverter(new FriendlyDiscordMessageConverter()); commands.RegisterConverter(new FriendlyBoolConverter()); }
/// <summary> /// Run the bot. Currently, this method never returns controll to the calling class. This will not always be the case, but /// </summary> /// <param name="botToken"></param> /// <returns></returns> public override async Task Run(string botToken) { DiscordClient discord = new DiscordClient(new DiscordConfiguration() { Token = botToken, TokenType = TokenType.Bot, Intents = DiscordIntents.All }); discord.GuildDownloadCompleted += OnGuildDownloadComplete; IServiceCollection services = new ServiceCollection().AddSingleton(guildDataReference); CommandsNextExtension commands = discord.UseCommandsNext(new CommandsNextConfiguration() { StringPrefixes = new[] { "!" }, EnableMentionPrefix = false, Services = services.BuildServiceProvider() }); //discord.GuildMemberAdded += MemberValidation.TrackMemberJoinGuild; //discord.GuildMemberRemoved += MemberValidation.TrackMemberLeaveGuild; //commands.RegisterCommands<ModerationModule>(); //commands.RegisterCommands<MusicModule>(); commands.RegisterCommands <VoiceChannelModule>(); commands.RegisterConverter(new ServerRegionConverter()); commands.RegisterConverter(new ChannelPublicityConverter()); commands.RegisterConverter(new PermitDenyStringToBoolConverter()); await discord.ConnectAsync(); //TODO figure out why this isnt authenticated and fix it //await discord.UpdateStatusAsync(new DiscordActivity { Name = "streaming development", ActivityType = ActivityType.Streaming, StreamUrl = "https://twitch.tv/not__nugget"}, UserStatus.Online); //TODO need to find a better way to return control without terminating an application await Task.Delay(-1); }
public static void RegisterConverters(this CommandsNextExtension cnext, Assembly?assembly = null) { assembly ??= Assembly.GetExecutingAssembly(); Type argConvType = typeof(IArgumentConverter); IEnumerable <Type> converterTypes = assembly .GetTypes() .Where(t => argConvType.IsAssignableFrom(t) && !t.IsAbstract && !t.IsInterface) ; foreach (Type converterType in converterTypes) { try { object?converterInstance = Activator.CreateInstance(converterType); if (converterInstance is { }) { cnext.RegisterConverter((dynamic)converterInstance); Log.Debug("Registered converter: {Converter}", converterType.Name); } } catch {
public static void RegisterAll(this CommandsNextExtension client) { client.RegisterCommands <Administration>(); client.RegisterCommands <ImageBoards>(); client.RegisterCommands <Japan>(); client.RegisterCommands <Language>(); client.RegisterCommands <LocalStats>(); client.RegisterCommands <Math>(); client.RegisterCommands <Minigames>(); client.RegisterCommands <Misc>(); client.RegisterCommands <Money>(); client.RegisterCommands <PublicStats>(); client.RegisterCommands <Quotes>(); client.RegisterCommands <ReactionRoles>(); client.RegisterConverter(new BoardConv()); client.RegisterConverter(new BooruConv()); client.RegisterConverter(new CurrencyConv()); client.RegisterConverter(new DiscordNamedColorConverter()); client.RegisterConverter(new DoujinEnumConv()); client.RegisterConverter(new HashTypeConv()); client.RegisterConverter(new RpsOptionConv()); client.SetHelpFormatter <HelpFormatter>(); }
public async Task RunBotAsync() { // first, let's load our configuration file var json = ""; using (var fs = File.OpenRead("config.json")) using (var sr = new StreamReader(fs, new UTF8Encoding(false))) json = await sr.ReadToEndAsync(); // next, let's load the values from that file // to our client's configuration var cfgjson = JsonConvert.DeserializeObject <ConfigJson>(json); var cfg = new DiscordConfiguration { Token = cfgjson.Token, TokenType = TokenType.Bot, AutoReconnect = true, MinimumLogLevel = LogLevel.Debug, }; // then we want to instantiate our client this.Client = new DiscordClient(cfg); // next, let's hook some events, so we know // what's going on this.Client.Ready += this.Client_Ready; this.Client.GuildAvailable += this.Client_GuildAvailable; this.Client.ClientErrored += this.Client_ClientError; // up next, let's set up our commands var ccfg = new CommandsNextConfiguration { // let's use the string prefix defined in config.json StringPrefixes = new[] { cfgjson.CommandPrefix }, // enable responding in direct messages EnableDms = true, // enable mentioning the bot as a command prefix EnableMentionPrefix = true }; // and hook them up this.Commands = this.Client.UseCommandsNext(ccfg); // let's hook some command events, so we know what's // going on this.Commands.CommandExecuted += this.Commands_CommandExecuted; this.Commands.CommandErrored += this.Commands_CommandErrored; // let's add a converter for a custom type and a name var mathopcvt = new MathOperationConverter(); Commands.RegisterConverter(mathopcvt); Commands.RegisterUserFriendlyTypeName <MathOperation>("operation"); // up next, let's register our commands this.Commands.RegisterCommands <ExampleUngrouppedCommands>(); this.Commands.RegisterCommands <ExampleGrouppedCommands>(); this.Commands.RegisterCommands <ExampleExecutableGroup>(); // set up our custom help formatter this.Commands.SetHelpFormatter <SimpleHelpFormatter>(); // finally, let's connect and log in await this.Client.ConnectAsync(); // when the bot is running, try doing <prefix>help // to see the list of registered commands, and // <prefix>help <command> to see help about specific // command. // and this is to prevent premature quitting await Task.Delay(-1); }
private void RegisterConvertors() { _commandService.RegisterConverter(_entityConvertor.ChannelConvertor); _commandService.RegisterConverter(_entityConvertor.UserConvertor); }
private async Task MainAsync() { // getting versions and assembly info SetBotVersionInfo(); // init log channels _logChannels = new List <DiscordChannel>(); _lastLogChWrite = File.GetLastWriteTime("logchannels.txt"); string json = await FileHandler.ReadJsonConfig(); if (json.Length == 0) { return; } if (json == "default") { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("Created default config file.\n" + "Now you need to get your discord bot token " + "and put it in config.json file.\n" + "Also make sure you set other parameters."); Console.ResetColor(); return; } // setting up client var cfgjson = JsonSerializer.Deserialize <ConfigJson>(json); if (cfgjson == null) { return; } var cfg = new DiscordConfiguration { Token = cfgjson.Token, TokenType = TokenType.Bot, AutoReconnect = true, MinimumLogLevel = RuntimeInfo == "Debug" ? LogLevel.Debug : LogLevel.Information, MessageCacheSize = 2048, LogTimestampFormat = "dd-MM-yyyy HH:mm:ss zzz" }; // client init and event hooks _discord = new DiscordClient(cfg); _discord.Ready += Discord_Ready; _discord.GuildAvailable += Discord_GuildAvailable; _discord.GuildUnavailable += Discord_GuildUnavailable; _discord.GuildCreated += Discord_GuildCreated; _discord.GuildDeleted += Discord_GuildDeleted; _discord.ChannelDeleted += Discord_ChannelDeleted; _discord.DmChannelDeleted += Discord_DmChannelDeleted; _discord.GuildDownloadCompleted += Discord_GuildDownloadCompleted; _discord.ClientErrored += Discord_ClientErrored; _discord.SocketClosed += Discord_SocketClosed; _discord.Resumed += Discord_Resumed; _discord.Heartbeated += Discord_Heartbeated; // setting up interactivity var intcfg = new InteractivityConfiguration { Timeout = TimeSpan.FromMinutes(cfgjson.ActTimeout), PaginationDeletion = PaginationDeletion.DeleteMessage, PollBehaviour = PollBehaviour.KeepEmojis }; _discord.UseInteractivity(intcfg); // setting up commands var cmdcfg = new CommandsNextConfiguration { StringPrefixes = new List <string> { cfgjson.CommandPrefix }, EnableDms = cfgjson.DmsEnabled, EnableMentionPrefix = cfgjson.MentionEnabled, CaseSensitive = cfgjson.CaseSensitive, EnableDefaultHelp = true }; // commands hooks and register _commands = _discord.UseCommandsNext(cmdcfg); _commands.CommandExecuted += Commands_CommandExecuted; _commands.CommandErrored += Commands_CommandErrored; _commands.RegisterCommands <Commands.Commands>(); _commands.RegisterCommands <LeetCommands>(); _commands.RegisterCommands <Interactivities.Interactivities>(); _commands.RegisterCommands <Administrative>(); _commands.RegisterCommands <Cats>(); _commands.RegisterCommands <DiceRolling>(); _commands.RegisterCommands <CryptoAes>(); _commands.RegisterCommands <CryptoRsa>(); _commands.RegisterCommands <MathCommands>(); _commands.RegisterCommands <StatusCommands>(); _commands.RegisterCommands <VoiceCommands>(); // adding math converter for custom type and name var mathopscvrt = new MathOperationConverter(); _commands.RegisterConverter(mathopscvrt); _commands.RegisterUserFriendlyTypeName <MathOperation>("operator"); // setting up and enabling voice var vcfg = new VoiceNextConfiguration { AudioFormat = AudioFormat.Default, EnableIncoming = false }; _discord.UseVoiceNext(vcfg); // setting custom help formatter _commands.SetHelpFormatter <HelpFormatter>(); // init twitch live and youtube video monitors _ttvApIclid = cfgjson.TwitchApiClid; _ttvApIsecret = cfgjson.TwitchApiSecret; _ytApIkey = cfgjson.YoutubeApiKey; _tlm = new TwitchLiveMonitor(); _yvm = new YoutubeVideoMonitor(); // getting aes and rsa keys from config AesKey = cfgjson.AesKey; AesIv = cfgjson.AesIv; RsaPublicKey = cfgjson.RsaPublicKey; RsaPrivateKey = cfgjson.RsaPrivateKey; // connecting discord try { await _discord.ConnectAsync(); } catch (Exception e) { _discord.Logger.LogCritical($"{e.Message}"); return; } await Task.Delay(-1); }
public Bot() { if (!File.Exists(CONFIG_PATH)) { new Config().SaveToFile(CONFIG_PATH); Console.WriteLine($"{CONFIG_PATH} not found, new one has been generated. Please fill in your info"); return; } Config = Config.LoadFromFile(CONFIG_PATH); // setup discord client _client = new DiscordClient(new DiscordConfiguration() { Token = Config.Token, TokenType = TokenType.Bot, AutoReconnect = true, GatewayCompressionLevel = GatewayCompressionLevel.Stream, }); // setup commands _commands = _client.UseCommandsNext(new CommandsNextConfiguration() { StringPrefixes = new string[] { Config.Prefix }, CaseSensitive = false, EnableDms = true, EnableDefaultHelp = false, }); // setup interactivity _interactivity =_client.UseInteractivity(new InteractivityConfiguration() { Timeout = TimeSpan.FromSeconds(30) }); // setup voice _voice = _client.UseVoiceNext(new VoiceNextConfiguration() { EnableIncoming = false, }); // setup custom extensions _client.AddExtension(new BotContextExtension(this)); _client.AddExtension(new EmojiExtension()); _client.AddExtension(new DownloaderExtension()); _client.AddExtension(new BooruExtension()); _client.AddExtension(new ConverterExtension()); _client.AddExtension(new SoundEffectWatcherExtension("./soundEffects")); _client.AddExtension(new YoutubeAPIExtension(Config.GoogleApiKey)); // setup custom converters _commands.RegisterConverter(new RegionConverter()); // hook events _client.Ready += Client_Ready; _client.SocketOpened += Client_SocketOpened; _client.SocketClosed += Client_SocketClosed; _client.SocketErrored += Client_SocketErrored; _client.ClientErrored += Client_ClientError; _commands.CommandExecuted += Commands_CommandExecuted; _commands.CommandErrored += Commands_CommandErrored; // add commands _commands.RegisterCommands(Assembly.GetExecutingAssembly()); }
public async Task RunAsync() { Log.Logger = new LoggerConfiguration() .WriteTo.Console() .MinimumLevel.Debug() .CreateLogger(); var logFactory = new LoggerFactory().AddSerilog(); var config = new DiscordConfiguration { Token = Configuration.Token, TokenType = TokenType.Bot, AutoReconnect = true, LoggerFactory = logFactory, Intents = DiscordIntents.All }; Client = new DiscordClient(config); Client.Ready += OnClientReady; Client.MessageCreated += OnMessage; Client.UseInteractivity(new InteractivityConfiguration { Timeout = TimeSpan.FromMinutes(5), PollBehaviour = PollBehaviour.KeepEmojis, PaginationDeletion = PaginationDeletion.DeleteMessage }); Services = new ServiceCollection() .AddDbContext <MUNContext>() .AddSingleton(this) .AddSingleton(Configuration) .AddSingleton <Guilds>() .AddSingleton <ProfanityFilterService>() .AddSingleton <PollService>() .BuildServiceProvider(); var commandsConfig = new CommandsNextConfiguration { StringPrefixes = new string[] { Configuration.Prefix }, EnableMentionPrefix = true, EnableDms = false, Services = Services }; Commands = Client.UseCommandsNext(commandsConfig); Commands.SetHelpFormatter <MUNHelp>(); Commands.RegisterConverter(new ChitConverter()); Commands.RegisterCommands <ConfigurationModule>(); Commands.RegisterCommands <GeneralModule>(); Commands.RegisterCommands <TimerModule>(); Commands.RegisterCommands <ModerationModule>(); Commands.RegisterCommands <CountryModule>(); Commands.RegisterCommands <MotionModule>(); Commands.RegisterCommands <PointsModule>(); Commands.CommandErrored += OnError; await Client.ConnectAsync(); await Task.Delay(-1); }