コード例 #1
0
        private async Task BackgroundTempChannelExpiryCheck(SocketGuild g, GuildInformation info)
        {
            SocketGuildChannel ch = null;

            lock (info)
            {
                ch = info.GetTemporaryChannel(g);
                if (ch == null)
                {
                    return;             // No temporary channel. Nothing to do.
                }
                if (!info.IsTempChannelExpired())
                {
                    return;
                }

                // If we got this far, the channel's expiring. Start the voting cooldown.
                info.Voting.StartCooldown();
            }
            await ch.DeleteAsync();
        }
コード例 #2
0
        private async Task BackgroundVoteSessionExpiryCheck(SocketGuild g, GuildInformation info)
        {
            bool   act;
            string nameTest;

            lock (info)
            {
                act      = info.Voting.IsSessionExpired();
                nameTest = info.Config.VoteChannel;
                if (act)
                {
                    info.Voting.StartCooldown();
                }
            }

            if (!act)
            {
                return;
            }
            // Determine the voting channel; will send announcement there.
            SocketTextChannel outCh = null;

            foreach (var ch in g.TextChannels)
            {
                if (string.Equals(ch.Name, nameTest, StringComparison.InvariantCultureIgnoreCase))
                {
                    outCh = ch;
                    break;
                }
            }
            if (outCh == null)
            {
                // Huh. Bad config?
                return;
            }
            await outCh.SendMessageAsync(":x: Not enough votes were placed for channel creation.");
        }
コード例 #3
0
        /// <summary>
        /// Helper method for VoteChecking. Does actual channel creation processing.
        /// </summary>
        private async Task <RestTextChannel> CreateTemporaryChannel(SocketGuild g, GuildInformation info)
        {
            // Yep, we're locking again.
            string     newChannelName;
            EntityList newChannelModlist;

            lock (info)
            {
                newChannelName    = info.Config.TempChannelName;
                newChannelModlist = info.Config.VoteStarters;
            }

            var newChannel = await g.CreateTextChannelAsync(newChannelName); // exceptions here are handled by caller

            foreach (var item in newChannelModlist.Roles)
            {
                // Evaluate role from data
                SocketRole r = null;
                if (item.Id.HasValue)
                {
                    r = g.GetRole(item.Id.Value);
                }
                if (r == null && item.Name != null)
                {
                    r = g.Roles.FirstOrDefault(gr => string.Equals(gr.Name, item.Name, StringComparison.OrdinalIgnoreCase));
                }
                if (r == null)
                {
                    await newChannel.SendMessageAsync($"Unable to find role `{item.ToString()}` to apply permissions.");

                    continue;
                }

                try
                {
                    await newChannel.AddPermissionOverwriteAsync(r, new OverwritePermissions(
                                                                     manageChannel : PermValue.Allow,
                                                                     sendMessages : PermValue.Allow,
                                                                     manageMessages : PermValue.Allow,
                                                                     manageRoles : PermValue.Allow));
                }
                catch (Discord.Net.HttpException ex)
                {
                    // TODO what error code are we looking for here? adjust to pick up only that.
                    // TODO clean up the error message display. users don't want to see the gory details.
                    await newChannel.SendMessageAsync($":x: Unable to set on `{r.Name}`: {ex.Message}");
                }
            }
            foreach (var item in newChannelModlist.Users)
            {
                // Evaluate user from data
                SocketUser u = null;
                if (item.Id.HasValue)
                {
                    u = g.GetUser(item.Id.Value);
                }
                if (u == null && item.Name != null)
                {
                    u = g.Users.FirstOrDefault(gu => string.Equals(gu.Username, item.Name, StringComparison.OrdinalIgnoreCase));
                }
                if (u == null)
                {
                    await newChannel.SendMessageAsync($"Unable to find user `{item.ToString()}` to apply permissions.");

                    continue;
                }

                try
                {
                    await newChannel.AddPermissionOverwriteAsync(u, new OverwritePermissions(
                                                                     manageChannel : PermValue.Allow,
                                                                     sendMessages : PermValue.Allow,
                                                                     manageMessages : PermValue.Allow,
                                                                     manageRoles : PermValue.Allow));
                }
                catch (Discord.Net.HttpException ex)
                {
                    // TODO same as above. which code do we want to catch specifically?
                    await newChannel.SendMessageAsync($":x: Unable to set on `{u.Username}`: {ex.Message}");
                }
            }
            return(newChannel);
        }