示例#1
0
        public override async Task Execute(Bot bot, DiscordUser user, DiscordMessage message, string[] args)
        {
            if (!Directory.Exists("out"))
            {
                Directory.CreateDirectory("out");
            }

            var assoc = new ColoredRoleAssociations();

            if (File.Exists(colorsPath))
            {
                while (true)
                {
                    try {
                        assoc = (ColoredRoleAssociations)JsonConvert.DeserializeObject <ColoredRoleAssociations>(File.ReadAllText(colorsPath));
                        break;
                    }
                    catch (IOException e) {
                        // Retry
                        Log.Warn("Waiting for file lock READ on " + colorsPath + "\n" + e.ToString());
                        await Task.Delay(100);
                    }
                }
            }

            var guildMember = await message.Channel.Guild.GetMemberAsync(user.Id);

            var         fetchedRoles = guildMember.Roles.Where(o => o.Id == assoc[user.Id]?.roleId);
            DiscordRole currentRole;

            if (fetchedRoles.Count() > 0)
            {
                currentRole = fetchedRoles.ElementAt(0);

                await guildMember.RevokeRoleAsync(currentRole);

                await guildMember.Guild.DeleteRoleAsync(currentRole);
            }

            assoc.ClearFor(user.Id);

            while (true)
            {
                try {
                    File.WriteAllText(colorsPath, JsonConvert.SerializeObject(assoc));
                    break;
                }
                catch (IOException e) {
                    Log.Warn("Waiting for file lock WRITE on " + colorsPath + "\n" + e.ToString());
                    await Task.Delay(100);
                }
            }

            await message.CreateReactionAsync(Extensions.ToDiscordEmoji("✅", bot));
        }
示例#2
0
        void RegisterEvents(DiscordClient client)
        {
            client.MessageReactionAdded += async react =>
            {
                if (react.Message == roleOnReact.message)
                {
                    var users   = new List <DiscordMember>(await react.Message.Channel.Guild.GetAllMembersAsync());
                    var reacter = users.Find(o => o.Id == react.User.Id);
                    var emoji   = react.Emoji;

                    if (roleOnReact.ContainsKey(emoji))
                    {
                        await reacter.GrantRoleAsync(roleOnReact[emoji]);
                    }
                }
            };

            client.MessageReactionRemoved += async react =>
            {
                if (react.Message == roleOnReact.message)
                {
                    var users   = new List <DiscordMember>(await react.Message.Channel.Guild.GetAllMembersAsync());
                    var reacter = users.Find(o => o.Id == react.User.Id);
                    var emoji   = react.Emoji;

                    if (roleOnReact.ContainsKey(emoji))
                    {
                        await reacter.RevokeRoleAsync(roleOnReact[emoji]);
                    }
                }
            };

            // Update role color postiion
            client.GuildRoleCreated += async roleCreate => {
                var assoc = new ColoredRoleAssociations();
                var path  = "out/colors.json";
                if (File.Exists(path))
                {
                    while (true)
                    {
                        try {
                            assoc = JsonConvert.DeserializeObject <ColoredRoleAssociations>(File.ReadAllText(path));
                            break;
                        }
                        catch (IOException e) {
                            Log.Warn("Waiting for file lock READ on " + path + "\n" + e.ToString());
                            await Task.Delay(100);
                        }
                    }
                }

                var isOurs = assoc.FindAll(o => o.roleId == roleCreate.Role.Id).Count > 0;
                if (isOurs)
                {
                    var me = await roleCreate.Guild.GetMemberAsync(client.CurrentUser.Id);

                    var myRole = me.Roles.OrderByDescending(o => o.Position).First();
                    var index  = myRole.Position - 1;
                    await me.Guild.UpdateRolePositionAsync(roleCreate.Role, index);

                    Log.Trace("Put role " + roleCreate.Role.Name + ":" + roleCreate.Role.Id + " to position " + index);
                }
            };

            client.ClientErrored += Client_ClientErrored;

            client.MessageCreated += async messageCreate =>
            {
                foreach (var content in messageCreate.Message.Content.Split("\n"))
                {
                    if (!content.StartsWith(prefix))
                    {
                        return;
                    }

                    var parts   = content.ToLower().Trim().Remove(0, 1).Split(" ");
                    var command = parts[0];

                    // +1 because the prefix is 1 character long
                    var args = content.Remove(0, command.Length + 1).Trim().Split(" ");
                    if (controller.ContainsKey(command))
                    {
                        Log.Trace(messageCreate.Author.Username + "#" + messageCreate.Author.Discriminator + ":" + messageCreate.Author.Id + " said: " + messageCreate.Message.Content);
                        try
                        {
                            await controller[command].Execute(
                                bot: this,
                                user: messageCreate.Author,
                                message: messageCreate.Message,
                                args: args
                                );
                        }
                        catch (IndexOutOfRangeException e)
                        {
                            Log.Warn("Wrong number of arguments for command: " + command + "(" + (args.Length) + ")\n" + e);
                        }
                        catch (ArgumentException e)
                        {
                            Log.Warn("Internal error for command: " + command + "(" + (args.Length) + ")\n" + e);
                        }
                    }
                }
            };
        }
示例#3
0
        public async override Task Execute(Bot bot, DiscordUser user, DiscordMessage message, string[] args)
        {
            if (!Directory.Exists("out"))
            {
                Directory.CreateDirectory("out");
            }

            var assoc = new ColoredRoleAssociations();

            if (File.Exists(colorsPath))
            {
                while (true)
                {
                    try {
                        assoc = JsonConvert.DeserializeObject <ColoredRoleAssociations>(File.ReadAllText(colorsPath));
                        break;
                    }
                    catch (IOException e) {
                        Log.Warn("Waiting for file lock READ on " + colorsPath + "\n" + e.ToString());
                        await Task.Delay(100);
                    }
                }
            }

            string colorName = args[0];
            string roleName  = args.Length > 1 ? string.Join(" ", args.Skip(1)) : colorName;


            DiscordColor color;

            try {
                color = new DiscordColor(colorName);
            }
            catch (Exception e) {
                await message.RespondAsync("Wrong color given! [" + colorName + "] is not a valid color. Use a #AABBCC format.");

                Log.Warn("When an user tried to set a color:\n" + e.ToString());
                return;
            }
            var guildMember = await message.Channel.Guild.GetMemberAsync(user.Id);

            var         fetchedRoles = guildMember.Roles.Where(o => o.Id == assoc[user.Id]?.roleId);
            DiscordRole currentRole;

            if (fetchedRoles.Count() > 0)
            {
                currentRole = fetchedRoles.ElementAt(0);
                await guildMember.Guild.UpdateRoleAsync(currentRole, roleName, hoist : false, color : color);
            }
            else
            {
                currentRole = await guildMember.Guild.CreateRoleAsync(roleName, DSharpPlus.Permissions.None, color, hoist : false, mentionable : false);
            }

            await guildMember.GrantRoleAsync(currentRole);

            assoc.ClearFor(user.Id);
            assoc.Add(new ColoredRoleAssociation()
            {
                roleId = currentRole.Id, userId = user.Id
            });

            while (true)
            {
                try {
                    File.WriteAllText(colorsPath, JsonConvert.SerializeObject(assoc));
                    break;
                }
                catch (IOException e) {
                    Log.Warn("Waiting for file lock WRITE on " + colorsPath + "\n" + e.ToString());
                    await Task.Delay(100);
                }
            }

            await message.CreateReactionAsync(Extensions.ToDiscordEmoji("✅", bot));

            Log.Info("Done creating role!");
        }