コード例 #1
0
ファイル: Core.cs プロジェクト: trameathia/DiscordBot
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(_Configuration.CurrentValue.DiscordToken))
            {
                throw new Exception("Please enter your bot's authorization token into the `appsettings.json` file.");
            }

            await _DiscordClient.LoginAsync(TokenType.Bot, _Configuration.CurrentValue.DiscordToken);

            await _DiscordClient.StartAsync();

            _CommandService.AddTypeReader(typeof(Dice), new DiceTypeReader());

            await _CommandService.AddModulesAsync(Assembly.GetEntryAssembly(), _ServiceProvider);
        }
コード例 #2
0
ファイル: StartupService.cs プロジェクト: CheAle14/casino-bot
        public async Task StartAsync(IServiceProvider provider)
        {
            string discordToken = _config["tokens:discord"];     // Get the discord token from the config file

            if (string.IsNullOrWhiteSpace(discordToken))
            {
                throw new Exception("Please enter your bot's token into the `_configuration.json` file found in the applications root directory.");
            }
            try
            {
                await _discord.LoginAsync(TokenType.Bot, discordToken);     // Login to discord

                await _discord.StartAsync();                                // Connect to the websocket
            }
            catch (System.Net.Http.HttpRequestException ex)
            {
                LogMsg("HTTP exception upon startup: " + ex.Message);
                File.WriteAllText("errlog.txt", ex.ToString());
                ReTryConnection();
            }
            // Add a LOT of event listeners.
            // Some are added twice (eg MessageDeleted, UserJoin) because
            // I want to do different things with them
            _discord.Ready        += Ready;
            _discord.Disconnected += _discord_Disconnected;

            // User Logs
            _discord.UserJoined   += UserJoin;
            _discord.UserJoined   += UserJoinCheckInvites;
            _discord.UserJoined   += _discord_UserJoined; // embd
            _discord.UserLeft     += UserLeft;
            _discord.UserLeft     += _discord_UserLeft;
            _discord.UserBanned   += _discord_UserBanned;
            _discord.UserUnbanned += _discord_UserUnbanned;
            _discord.UserUpdated  += _discord_UserUpdated;
            // Guild member
            _discord.GuildMemberUpdated += _discord_GuildMemberUpdated; // embd
            _discord.GuildMemberUpdated += CasinoUserUpdatedCheckNickname;
            // Messages
            _discord.ReactionAdded   += ReactionMessageHandler;
            _discord.MessageReceived += MessageRecieved;
            _discord.ReactionAdded   += _discord_ReactionAdded;
            _discord.MessageUpdated  += MessageUpdated;
            _discord.MessageUpdated  += _discord_MessageUpdated;
            _discord.MessageDeleted  += MessageDeleted;
            _discord.MessageDeleted  += _discord_MessageDeleted;
            // Channels
            _discord.ChannelUpdated   += _discord_ChannelUpdated;   // embd
            _discord.ChannelCreated   += _discord_ChannelCreated;   // embd
            _discord.ChannelDestroyed += _discord_ChannelDestroyed; // embd
            // Roles
            _discord.RoleUpdated += _discord_RoleUpdated;           // embd
            _discord.RoleCreated += _discord_RoleCreated;           // embd
            _discord.RoleDeleted += _discord_RoleDeleted;           // embd
            // Guilds
            _discord.GuildUpdated += _discord_GuildUpdated;

            Program._discord_ = _discord;
            // add typereaders, so you can have a function eg:
            // public async Task DoSomeCommand(SocketGuildUser user) { .... }
            //      or
            // public async Task DoAnotherCommand(CasinoMember member) { .... }
            // and you don't have to parse a string for *every* command

            _commands.AddTypeReader(typeof(SocketGuildUser), new SocketGuildUserTypeReader());
            _commands.AddTypeReader(typeof(CasinoMember), new CasinoMemberTypeReader());
            _commands.AddTypeReader(typeof(EduLinkRPC.Classes.HwkUser), new HwkUserTypeReader());
            ServiceProvider = provider;
        }