public static async Task OnGuildMemberAdded(DiscordClient client, GuildMemberAddEventArgs e) { if (e.Guild.Id != Config.serverID) { return; } // Check if user has registered their Github account if (!Database.TryGetSponsor(e.Member.Id, out Database.SponsorEntry sponsorEntry)) { return; } // Check if user is active sponsor List <Github.Sponsor> sponsors = await Github.GetSponsors(); Github.Sponsor sponsor = sponsors.FirstOrDefault(s => s.sponsor.id == sponsorEntry.githubID); if (sponsor == null) { return; } // Check if tier is registered in the config if (!Config.TryGetTierRole(sponsor.dollarAmount, out ulong roleID)) { return; } // Assign role DiscordRole role = e.Guild.GetRole(roleID); Logger.Log(LogID.Discord, Utils.FullName(e.Member) + " (" + e.Member.Id + ") were given back the role '" + role.Name + "' on rejoin. "); await e.Member.GrantRoleAsync(role); }
public static async Task RunSponsorCheck() { DiscordGuild guild; try { guild = await client.GetGuildAsync(Config.serverID); } catch (Exception) { Logger.Error(LogID.Discord, "Error could not find Discord server with the configured id '" + Config.serverID + "'"); return; } List <Github.Sponsor> sponsors = await Github.GetSponsors(); List <Database.SponsorEntry> linkedUsers = Database.GetAllSponsors(); foreach (Database.SponsorEntry linkedUser in linkedUsers) { DiscordMember member; try { member = await guild.GetMemberAsync(linkedUser.discordID); if (member == null) { throw new Exception(); } } catch (Exception) { Logger.Warn(LogID.Discord, "Could not find user with id '" + linkedUser.discordID + "' on the server."); continue; } Logger.Log(LogID.Discord, "Checking member: " + Utils.FullName(member)); ulong sponsorTierRoleID = 0; Github.Sponsor sponsor = sponsors.FirstOrDefault(x => x.sponsor.id == linkedUser.githubID); if (sponsor != null) { sponsorTierRoleID = Config.tierRoles.GetValueOrDefault(sponsor.dollarAmount); } await SyncRoles(member, sponsorTierRoleID); } }
public static async void Initialize() { Logger.Log(LogID.General, "Loading config \"" + Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "config.yml\""); Config.LoadConfig(); // Check if bot token is unset if (Config.botToken == "<add-token-here>" || string.IsNullOrWhiteSpace(Config.botToken)) { Logger.Fatal(LogID.Config, "You need to set your bot token in the config and start the bot again."); throw new ArgumentException("Invalid Discord bot token"); } // Check if github token is unset if (Config.githubToken == "<add-token-here>" || string.IsNullOrWhiteSpace(Config.githubToken)) { Logger.Fatal(LogID.Config, "You need to set your Github personal access token in the config and start the bot again."); throw new ArgumentException("Invalid Github personal access token"); } // Database connection and setup try { Logger.Log(LogID.General, "Connecting to database..."); Database.Initialize(); } catch (Exception e) { Logger.Fatal(LogID.General, "Could not set up database tables, please confirm connection settings, status of the server and permissions of MySQL user. Error: " + e); throw; } Logger.Log(LogID.General, "Setting up Github API client..."); Github.Initialize(); Logger.Log(LogID.General, "Setting up Discord client..."); // Checking log level if (!Enum.TryParse(Config.logLevel, true, out LogLevel logLevel)) { Logger.Log(LogID.General, "Log level " + Config.logLevel + " invalid, using 'Information' instead."); logLevel = LogLevel.Information; } // Setting up client configuration DiscordConfiguration cfg = new DiscordConfiguration { Token = Config.botToken, TokenType = TokenType.Bot, MinimumLogLevel = logLevel, AutoReconnect = true, Intents = DiscordIntents.All }; discordClient = new DiscordClient(cfg); Logger.Log(LogID.General, "Hooking commands..."); CommandsNextExtension commands = discordClient.UseCommandsNext(new CommandsNextConfiguration { StringPrefixes = new[] { Config.prefix } }); commands.RegisterCommands <LinkCommand>(); commands.RegisterCommands <UnlinkCommand>(); commands.RegisterCommands <RecheckCommand>(); Logger.Log(LogID.General, "Hooking events..."); discordClient.Ready += EventHandler.OnReady; discordClient.GuildAvailable += EventHandler.OnGuildAvailable; discordClient.ClientErrored += EventHandler.OnClientError; discordClient.GuildMemberAdded += EventHandler.OnGuildMemberAdded; commands.CommandErrored += EventHandler.OnCommandError; Logger.Log(LogID.General, "Connecting to Discord..."); await discordClient.ConnectAsync(); RoleChecker.RunPeriodically(); }