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);
        }
Exemple #2
0
        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);
            }
        }