From e9c6f5b0cb39284bb0af086892dcf08d4779e2f6 Mon Sep 17 00:00:00 2001 From: Madpeterz <57852827+Madpeterz@users.noreply.github.com> Date: Sat, 30 Jan 2021 14:34:24 +0000 Subject: [PATCH] Testing the new discord API commands --- CLI/DiscordSupervisor/Discord-super.cs | 1 + Core/Commands/Avatars/NearMe.cs | 2 +- Core/Commands/DiscordAPI/AddRole.cs | 53 ++++++ Core/Commands/DiscordAPI/Ban_Member.cs | 54 ++++++ .../Commands/DiscordAPI/BulkClear_Messages.cs | 75 ++++++++ Core/Commands/DiscordAPI/Dm_Member.cs | 45 +++++ Core/Commands/DiscordAPI/MembersList.cs | 58 ++++++ Core/Commands/DiscordAPI/MessageChannel.cs | 65 +++++++ Core/Commands/DiscordAPI/MuteMember.cs | 57 ++++++ Core/Commands/DiscordAPI/RoleCreate.cs | 47 +++++ Core/Commands/DiscordAPI/RoleList.cs | 49 ++++++ Core/Commands/DiscordAPI/RoleRemove.cs | 49 ++++++ Core/Commands/DiscordAPI/Role_GetSettings.cs | 84 +++++++++ Core/Commands/DiscordAPI/Role_UpdatePerms.cs | 165 ++++++++++++++++++ Core/Commands/DiscordAPI/TextChannels_List.cs | 49 ++++++ Core/bottypes/VirtualsBot.cs | 32 +++- 16 files changed, 881 insertions(+), 4 deletions(-) create mode 100644 Core/Commands/DiscordAPI/AddRole.cs create mode 100644 Core/Commands/DiscordAPI/Ban_Member.cs create mode 100644 Core/Commands/DiscordAPI/BulkClear_Messages.cs create mode 100644 Core/Commands/DiscordAPI/Dm_Member.cs create mode 100644 Core/Commands/DiscordAPI/MembersList.cs create mode 100644 Core/Commands/DiscordAPI/MessageChannel.cs create mode 100644 Core/Commands/DiscordAPI/MuteMember.cs create mode 100644 Core/Commands/DiscordAPI/RoleCreate.cs create mode 100644 Core/Commands/DiscordAPI/RoleList.cs create mode 100644 Core/Commands/DiscordAPI/RoleRemove.cs create mode 100644 Core/Commands/DiscordAPI/Role_GetSettings.cs create mode 100644 Core/Commands/DiscordAPI/Role_UpdatePerms.cs create mode 100644 Core/Commands/DiscordAPI/TextChannels_List.cs diff --git a/CLI/DiscordSupervisor/Discord-super.cs b/CLI/DiscordSupervisor/Discord-super.cs index cd91add..f5a40dd 100644 --- a/CLI/DiscordSupervisor/Discord-super.cs +++ b/CLI/DiscordSupervisor/Discord-super.cs @@ -69,6 +69,7 @@ namespace BetterSecondBot.DiscordSupervisor protected override void login_bot(bool self_keep_alive) { controler = new CliExitOnLogout(myconfig, running_as_docker, self_keep_alive); + controler.Bot.setDiscordClient(DiscordClient); attach_events(); } diff --git a/Core/Commands/Avatars/NearMe.cs b/Core/Commands/Avatars/NearMe.cs index abed94f..f99447b 100644 --- a/Core/Commands/Avatars/NearMe.cs +++ b/Core/Commands/Avatars/NearMe.cs @@ -7,7 +7,7 @@ using Newtonsoft.Json; namespace BSB.Commands.Avatars { - class NearMe : CoreCommand_SmartReply_1arg + class RoleCreate : CoreCommand_SmartReply_1arg { public override string Helpfile { get { return "Gets a list of avatars near the bot"; } } public override bool CallFunction(string[] args) diff --git a/Core/Commands/DiscordAPI/AddRole.cs b/Core/Commands/DiscordAPI/AddRole.cs new file mode 100644 index 0000000..e4043c0 --- /dev/null +++ b/Core/Commands/DiscordAPI/AddRole.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +using OpenMetaverse; +using Newtonsoft.Json; +using Discord.WebSocket; +using Discord; +using Discord.Rest; +using System.Threading.Tasks; + +namespace BSB.Commands.DiscordAPI +{ + class Discord_AddRole : CoreCommand_4arg + { + public override string[] ArgTypes { get { return new[] { "Smart", "Number", "Number", "Number" }; } } + public override string[] ArgHints { get { return new[] { "Mixed [Channel|Avatar uuid|Avatar name|http url]", "Discord server id", "Role id", "Member id" }; } } + public override string Helpfile { get { return "Adds the selected role to the member"; } } + public override bool CallFunction(string[] args) + { + if (base.CallFunction(args) == true) + { + if (bot.discordReady() == false) + { + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], bot.getDiscordWhyFailed(), CommandName); + } + bool reply = addRoleToMember(args).Result; + return bot.GetCommandsInterface.SmartCommandReply(reply, args[0], "see status for state", CommandName); + } + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], "incorrect args", CommandName); + } + + protected async Task addRoleToMember(string[] args) + { + DiscordSocketClient Discord = bot.getDiscordClient(); + if (ulong.TryParse(args[1], out ulong serverid) == true) + { + if(ulong.TryParse(args[2], out ulong roleid) == true) + { + if (ulong.TryParse(args[3], out ulong memberid) == true) + { + SocketGuild server = Discord.GetGuild(serverid); + SocketRole role = server.GetRole(roleid); + SocketGuildUser user = server.GetUser(memberid); + await user.AddRoleAsync(role); // ? Irole seems to accept SocketRole ? + return true; + } + } + } + return false; + } + } +} diff --git a/Core/Commands/DiscordAPI/Ban_Member.cs b/Core/Commands/DiscordAPI/Ban_Member.cs new file mode 100644 index 0000000..677fe65 --- /dev/null +++ b/Core/Commands/DiscordAPI/Ban_Member.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +using OpenMetaverse; +using Newtonsoft.Json; +using Discord.WebSocket; +using Discord; +using Discord.Rest; +using System.Threading.Tasks; + +namespace BSB.Commands.DiscordAPI +{ + class Discord_BanMember : CoreCommand_3arg + { + public override string[] ArgTypes { get { return new[] { "Smart", "Number", "Number","Text" }; } } + public override string[] ArgHints { get { return new[] { "Mixed [Channel|Avatar uuid|Avatar name|http url]", "Discord server id", "Member id","Optional why message" }; } } + public override string Helpfile { get { return "Adds the selected user to the ban list - Disallows rejoining untill they are removed from the list "; } } + public override bool CallFunction(string[] args) + { + if (base.CallFunction(args) == true) + { + if (bot.discordReady() == false) + { + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], bot.getDiscordWhyFailed(), CommandName); + } + bool reply = BanMember(args).Result; + return bot.GetCommandsInterface.SmartCommandReply(reply, args[0], "see status for state", CommandName); + } + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], "incorrect args", CommandName); + } + + protected async Task BanMember(string[] args) + { + DiscordSocketClient Discord = bot.getDiscordClient(); + if (ulong.TryParse(args[1], out ulong serverid) == true) + { + if (ulong.TryParse(args[2], out ulong memberid) == true) + { + SocketGuild server = Discord.GetGuild(serverid); + SocketGuildUser user = server.GetUser(memberid); + string why = "Secondbot API ban - see your own logs"; + if (args.Length == 4) + { + why = args[3]; + } + await user.BanAsync(7, why); + return true; + } + } + return false; + } + } +} diff --git a/Core/Commands/DiscordAPI/BulkClear_Messages.cs b/Core/Commands/DiscordAPI/BulkClear_Messages.cs new file mode 100644 index 0000000..f899f11 --- /dev/null +++ b/Core/Commands/DiscordAPI/BulkClear_Messages.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +using OpenMetaverse; +using Newtonsoft.Json; +using Discord.WebSocket; +using Discord; +using Discord.Rest; +using System.Threading.Tasks; + +namespace BSB.Commands.DiscordAPI +{ + class Discord_BulkClear_Messages : CoreCommand_3arg + { + public override string[] ArgTypes { get { return new[] { "Smart", "Number", "Number" }; } } + public override string[] ArgHints { get { return new[] { "Mixed [Channel|Avatar uuid|Avatar name|http url]", "Discord server id", "Member id" }; } } + public override string Helpfile { get { return "Clears messages on the server sent by the member in the last 13 days, 22hours 59mins"; } } + public override bool CallFunction(string[] args) + { + if (base.CallFunction(args) == true) + { + if (bot.discordReady() == false) + { + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], bot.getDiscordWhyFailed(), CommandName); + } + KeyValuePair reply = ClearMessages(args).Result; + Dictionary collection = new Dictionary(); + collection.Add("Deleted", reply.Value.ToString()); + return bot.GetCommandsInterface.SmartCommandReply(reply.Key, args[0], "See status for state, see collection for total deleted", CommandName, collection); + } + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], "incorrect args", CommandName); + } + + protected async Task> ClearMessages(string[] args) + { + int messagesDeleted = 0; + DiscordSocketClient Discord = bot.getDiscordClient(); + if (ulong.TryParse(args[1], out ulong serverid) == true) + { + if (ulong.TryParse(args[2], out ulong memberid) == true) + { + SocketGuild server = Discord.GetGuild(serverid); + SocketGuildUser user = server.GetUser(memberid); + TimeSpan somedays = new TimeSpan(13, 22, 59, 59, 0); + DateTimeOffset justundertwosweeks = DateTimeOffset.Now; + justundertwosweeks = justundertwosweeks.Subtract(somedays); + long unixtimelimit = justundertwosweeks.ToUnixTimeSeconds(); + foreach (SocketTextChannel channel in server.TextChannels) + { + List DeleteMessages = new List(); + IEnumerable messages = await channel.GetMessagesAsync(100).FlattenAsync(); + foreach (IMessage message in messages) + { + if (message.CreatedAt.ToUnixTimeSeconds() > unixtimelimit) + { + if (message.Author.Id == user.Id) + { + DeleteMessages.Add(message); + } + } + } + if(DeleteMessages.Count() > 0) + { + messagesDeleted += DeleteMessages.Count(); + await channel.DeleteMessagesAsync(DeleteMessages); + } + } + return new KeyValuePair(true, messagesDeleted); + } + } + return new KeyValuePair(false,0); + } + } +} diff --git a/Core/Commands/DiscordAPI/Dm_Member.cs b/Core/Commands/DiscordAPI/Dm_Member.cs new file mode 100644 index 0000000..526cb0d --- /dev/null +++ b/Core/Commands/DiscordAPI/Dm_Member.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +using OpenMetaverse; +using Newtonsoft.Json; +using Discord.WebSocket; +using Discord; +using Discord.Rest; +using System.Threading.Tasks; + +namespace BSB.Commands.DiscordAPI +{ + class Discord_Dm_Member : CoreCommand_3arg + { + public override string[] ArgTypes { get { return new[] { "Smart", "Number", "Text" }; } } + public override string[] ArgHints { get { return new[] { "Mixed [Channel|Avatar uuid|Avatar name|http url]", "Member id", "Message" }; } } + public override string Helpfile { get { return "Sends a message directly to the user \n This will fail if they are not friends or dont share a server!"; } } + public override bool CallFunction(string[] args) + { + if (base.CallFunction(args) == true) + { + if (bot.discordReady() == false) + { + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], bot.getDiscordWhyFailed(), CommandName); + } + bool reply = MessageMember(args).Result; + return bot.GetCommandsInterface.SmartCommandReply(reply, args[0], "see status for state", CommandName); + } + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], "incorrect args", CommandName); + } + + protected async Task MessageMember(string[] args) + { + DiscordSocketClient Discord = bot.getDiscordClient(); + if (ulong.TryParse(args[1], out ulong memberid) == true) + { + SocketUser user = Discord.GetUser(memberid); + await user.SendMessageAsync(args[2]); + return true; + } + return false; + } + } +} diff --git a/Core/Commands/DiscordAPI/MembersList.cs b/Core/Commands/DiscordAPI/MembersList.cs new file mode 100644 index 0000000..e5e5ae4 --- /dev/null +++ b/Core/Commands/DiscordAPI/MembersList.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +using OpenMetaverse; +using Newtonsoft.Json; +using Discord.WebSocket; +using Discord; +using Discord.Rest; +using System.Threading.Tasks; + +namespace BSB.Commands.DiscordAPI +{ + class Discord_MembersList : CoreCommand_SmartReply_2arg + { + public override string[] ArgTypes { get { return new[] { "Smart", "Number" }; } } + public override string[] ArgHints { get { return new[] { "Mixed [Channel|Avatar uuid|Avatar name|http url]", "Discord server id" }; } } + public override string Helpfile { get { return "Returns a list of members in a server \n collection is userid: username \n if the user has set a nickname: userid: nickname|username \n This command requires Discord full client mode enabled and connected\n !!!! This command also requires: Privileged Gateway Intents / SERVER MEMBERS INTENT set to true on the discord bot api area !!!"; } } + public override bool CallFunction(string[] args) + { + if (base.CallFunction(args) == true) + { + if (bot.discordReady() == false) + { + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], bot.getDiscordWhyFailed(), CommandName); + } + KeyValuePair> reply = ListMembers(args).Result; + return bot.GetCommandsInterface.SmartCommandReply(reply.Key, args[0], "see status for state", CommandName, reply.Value); + } + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], "incorrect args", CommandName); + } + + protected async Task>> ListMembers(string[] args) + { + DiscordSocketClient Discord = bot.getDiscordClient(); + if (ulong.TryParse(args[1], out ulong serverid) == true) + { + SocketGuild server = Discord.GetGuild(serverid); + IEnumerable users = await server.GetUsersAsync().FlattenAsync().ConfigureAwait(true); + Dictionary membersList = new Dictionary(); + + foreach (IGuildUser user in users) + { + if (user.Nickname != null) + { + membersList.Add(user.Id.ToString(), user.Nickname+"|"+user.Username); + } + else + { + membersList.Add(user.Id.ToString(), user.Username); + } + } + return new KeyValuePair>(true, membersList); + } + return new KeyValuePair>(false, new Dictionary()); + } + } +} diff --git a/Core/Commands/DiscordAPI/MessageChannel.cs b/Core/Commands/DiscordAPI/MessageChannel.cs new file mode 100644 index 0000000..82dd358 --- /dev/null +++ b/Core/Commands/DiscordAPI/MessageChannel.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +using OpenMetaverse; +using Newtonsoft.Json; +using Discord.WebSocket; +using Discord; +using Discord.Rest; +using System.Threading.Tasks; + +namespace BSB.Commands.DiscordAPI +{ + class Discord_MessageChannel : CoreCommand_4arg + { + public override string[] ArgTypes { get { return new[] { "Smart", "Number", "Number", "Message", "True|False" }; } } + public override string[] ArgHints { get { return new[] { "Mixed [Channel|Avatar uuid|Avatar name|http url]", "Discord server id", "channel id", "Message to send", "Use TTS defaults to False" }; } } + public override string Helpfile { get { return "Sends a message to the selected channel - Optional TTS usage"; } } + public override bool CallFunction(string[] args) + { + if (base.CallFunction(args) == true) + { + if (bot.discordReady() == false) + { + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], bot.getDiscordWhyFailed(), CommandName); + } + bool reply = MessageChannel(args).Result; + return bot.GetCommandsInterface.SmartCommandReply(reply, args[0], "see status for state", CommandName); + } + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], "incorrect args", CommandName); + } + + protected async Task MessageChannel(string[] args) + { + DiscordSocketClient Discord = bot.getDiscordClient(); + if (ulong.TryParse(args[1], out ulong serverid) == true) + { + if (ulong.TryParse(args[2], out ulong channelid) == true) + { + bool useTTS = false; + if(args.Length == 5) + { + bool.TryParse(args[4], out useTTS); + } + SocketGuild server = Discord.GetGuild(serverid); + SocketTextChannel Channel = null; + foreach (SocketTextChannel channel in server.TextChannels) + { + if(channel.Id == channelid) + { + Channel = channel; + break; + } + } + if(Channel != null) + { + await Channel.SendMessageAsync(args[3], useTTS); + return true; + } + } + } + return false; + } + } +} diff --git a/Core/Commands/DiscordAPI/MuteMember.cs b/Core/Commands/DiscordAPI/MuteMember.cs new file mode 100644 index 0000000..ecfa817 --- /dev/null +++ b/Core/Commands/DiscordAPI/MuteMember.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +using OpenMetaverse; +using Newtonsoft.Json; +using Discord.WebSocket; +using Discord; +using Discord.Rest; +using System.Threading.Tasks; + +namespace BSB.Commands.DiscordAPI +{ + class Discord_MuteMember : CoreCommand_3arg + { + public override string[] ArgTypes { get { return new[] { "Smart", "Number", "Number", "True|False" }; } } + public override string[] ArgHints { get { return new[] { "Mixed [Channel|Avatar uuid|Avatar name|http url]", "Discord server id", "Member id", "{Optional} Mute status (defaults to true) set this to False to unmute" }; } } + public override string Helpfile { get { return "Updates the Mute status of the member"; } } + public override bool CallFunction(string[] args) + { + if (base.CallFunction(args) == true) + { + if (bot.discordReady() == false) + { + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], bot.getDiscordWhyFailed(), CommandName); + } + bool reply = MuteMember(args).Result; + return bot.GetCommandsInterface.SmartCommandReply(reply, args[0], "see status for state", CommandName); + } + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], "incorrect args", CommandName); + } + + protected async Task MuteMember(string[] args) + { + DiscordSocketClient Discord = bot.getDiscordClient(); + if (ulong.TryParse(args[1], out ulong serverid) == true) + { + if (ulong.TryParse(args[2], out ulong memberid) == true) + { + SocketGuild server = Discord.GetGuild(serverid); + SocketGuildUser user = server.GetUser(memberid); + bool status = true; + if (args.Length == 4) + { + bool.TryParse(args[3], out status); + } + await user.ModifyAsync(pr => + { + pr.Mute = status; + }); + return true; + } + } + return false; + } + } +} diff --git a/Core/Commands/DiscordAPI/RoleCreate.cs b/Core/Commands/DiscordAPI/RoleCreate.cs new file mode 100644 index 0000000..6beae56 --- /dev/null +++ b/Core/Commands/DiscordAPI/RoleCreate.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +using OpenMetaverse; +using Newtonsoft.Json; +using Discord.WebSocket; +using Discord; +using Discord.Rest; +using System.Threading.Tasks; + +namespace BSB.Commands.DiscordAPI +{ + class Discord_RoleCreate : CoreCommand_SmartReply_3arg + { + public override string[] ArgTypes { get { return new[] { "Smart","Number","Text" }; } } + public override string[] ArgHints { get { return new[] { "Mixed [Channel|Avatar uuid|Avatar name|http url]","Discord server id","Role name" }; } } + public override string Helpfile { get { return "Creates a new role and returns its role ID via smart target\n This command requires Discord full client mode enabled and connected"; } } + public override bool CallFunction(string[] args) + { + if (base.CallFunction(args) == true) + { + if(bot.discordReady() == false) + { + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], bot.getDiscordWhyFailed(), CommandName); + } + KeyValuePair reply = CreateRole(args).Result; + Dictionary collection = new Dictionary(); + collection.Add("roleid", reply.Value.ToString()); + return bot.GetCommandsInterface.SmartCommandReply(true, args[0], "ok", CommandName, collection); + } + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], "incorrect args", CommandName); + } + + protected async Task> CreateRole(string[] args) + { + DiscordSocketClient Discord = bot.getDiscordClient(); + if (ulong.TryParse(args[1], out ulong serverid) == true) + { + SocketGuild server = Discord.GetGuild(serverid); + RestRole role = await server.CreateRoleAsync(args[2], new GuildPermissions(), Color.DarkRed, false, null).ConfigureAwait(true); + return new KeyValuePair("ok", role.Id); + } + return new KeyValuePair("Unable to process server id", 0); + } + } +} diff --git a/Core/Commands/DiscordAPI/RoleList.cs b/Core/Commands/DiscordAPI/RoleList.cs new file mode 100644 index 0000000..42aa85d --- /dev/null +++ b/Core/Commands/DiscordAPI/RoleList.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +using OpenMetaverse; +using Newtonsoft.Json; +using Discord.WebSocket; +using Discord; +using Discord.Rest; +using System.Threading.Tasks; + +namespace BSB.Commands.DiscordAPI +{ + class Discord_RoleList: CoreCommand_SmartReply_2arg + { + public override string[] ArgTypes { get { return new[] { "Smart", "Number" }; } } + public override string[] ArgHints { get { return new[] { "Mixed [Channel|Avatar uuid|Avatar name|http url]", "Discord server id" }; } } + public override string Helpfile { get { return "Returns a list of roles and their ids in collection \n This command requires Discord full client mode enabled and connected"; } } + public override bool CallFunction(string[] args) + { + if (base.CallFunction(args) == true) + { + if (bot.discordReady() == false) + { + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], bot.getDiscordWhyFailed(), CommandName); + } + KeyValuePair> reply = ListRoles(args); + return bot.GetCommandsInterface.SmartCommandReply(reply.Key, args[0], "see status for state", CommandName, reply.Value); + } + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], "incorrect args", CommandName); + } + + protected KeyValuePair> ListRoles(string[] args) + { + DiscordSocketClient Discord = bot.getDiscordClient(); + if (ulong.TryParse(args[1], out ulong serverid) == true) + { + SocketGuild server = Discord.GetGuild(serverid); + Dictionary rolesList = new Dictionary(); + foreach (SocketRole role in server.Roles) + { + rolesList.Add(role.Id.ToString(), role.Name); + } + return new KeyValuePair>(true, rolesList); + } + return new KeyValuePair>(false,new Dictionary()); + } + } +} diff --git a/Core/Commands/DiscordAPI/RoleRemove.cs b/Core/Commands/DiscordAPI/RoleRemove.cs new file mode 100644 index 0000000..fa75271 --- /dev/null +++ b/Core/Commands/DiscordAPI/RoleRemove.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +using OpenMetaverse; +using Newtonsoft.Json; +using Discord.WebSocket; +using Discord; +using Discord.Rest; +using System.Threading.Tasks; + +namespace BSB.Commands.DiscordAPI +{ + class Discord_RoleRemove: CoreCommand_SmartReply_3arg + { + public override string[] ArgTypes { get { return new[] { "Smart", "Number", "Number" }; } } + public override string[] ArgHints { get { return new[] { "Mixed [Channel|Avatar uuid|Avatar name|http url]", "Discord server id", "Role id" }; } } + public override string Helpfile { get { return "Remove a new role from a server \n This command requires Discord full client mode enabled and connected"; } } + public override bool CallFunction(string[] args) + { + if (base.CallFunction(args) == true) + { + if (bot.discordReady() == false) + { + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], bot.getDiscordWhyFailed(), CommandName); + } + bool status = RemoveRole(args).Result; + return bot.GetCommandsInterface.SmartCommandReply(status, args[0], "see status for removal state", CommandName); + } + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], "incorrect args", CommandName); + } + + protected async Task RemoveRole(string[] args) + { + DiscordSocketClient Discord = bot.getDiscordClient(); + if (ulong.TryParse(args[1], out ulong serverid) == true) + { + if (ulong.TryParse(args[2], out ulong roleid) == true) + { + SocketGuild server = Discord.GetGuild(serverid); + SocketRole role = server.GetRole(roleid); + await role.DeleteAsync().ConfigureAwait(true); + return true; + } + } + return false; + } + } +} diff --git a/Core/Commands/DiscordAPI/Role_GetSettings.cs b/Core/Commands/DiscordAPI/Role_GetSettings.cs new file mode 100644 index 0000000..01066ab --- /dev/null +++ b/Core/Commands/DiscordAPI/Role_GetSettings.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +using OpenMetaverse; +using Newtonsoft.Json; +using Discord.WebSocket; +using Discord; +using Discord.Rest; +using System.Threading.Tasks; + +namespace BSB.Commands.DiscordAPI +{ + class Discord_Role_GetSettings : CoreCommand_SmartReply_3arg + { + public override string[] ArgTypes { get { return new[] { "Smart", "Number", "Number" }; } } + public override string[] ArgHints { get { return new[] { "Mixed [Channel|Avatar uuid|Avatar name|http url]", "Discord server id", "Role id" }; } } + public override string Helpfile { get { return "returns a collection of settings for the given role \n This command requires Discord full client mode enabled and connected"; } } + public override bool CallFunction(string[] args) + { + if (base.CallFunction(args) == true) + { + if (bot.discordReady() == false) + { + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], bot.getDiscordWhyFailed(), CommandName); + } + KeyValuePair> reply = RoleSettings(args); + return bot.GetCommandsInterface.SmartCommandReply(reply.Key, args[0], "see status for state", CommandName,reply.Value); + } + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], "incorrect args", CommandName); + } + + protected KeyValuePair> RoleSettings(string[] args) + { + DiscordSocketClient Discord = bot.getDiscordClient(); + if (ulong.TryParse(args[1], out ulong serverid) == true) + { + if (ulong.TryParse(args[2], out ulong roleid) == true) + { + SocketGuild server = Discord.GetGuild(serverid); + SocketRole role = server.GetRole(roleid); + Dictionary settings = new Dictionary + { + { "Config|Color", String.Join(",", new byte[3] { role.Color.R, role.Color.G, role.Color.B }) }, + { "Config|Name", role.Name }, + { "Info|AssignedCount", role.Members.Count().ToString() }, + { "Perm|AttachFiles", role.Permissions.AttachFiles.ToString() }, + { "Perm|ReadMessageHistory", role.Permissions.ReadMessageHistory.ToString() }, + { "Perm|MentionEveryone", role.Permissions.MentionEveryone.ToString() }, + { "Perm|UseExternalEmojis", role.Permissions.UseExternalEmojis.ToString() }, + { "Perm|Connect", role.Permissions.Connect.ToString() }, + { "Perm|Speak", role.Permissions.Speak.ToString() }, + { "Perm|MuteMembers", role.Permissions.MuteMembers.ToString() }, + { "Perm|UseVAD", role.Permissions.UseVAD.ToString() }, + { "Perm|MoveMembers", role.Permissions.MoveMembers.ToString() }, + { "Perm|EmbedLinks", role.Permissions.EmbedLinks.ToString() }, + { "Perm|PrioritySpeaker", role.Permissions.PrioritySpeaker.ToString() }, + { "Perm|Stream", role.Permissions.Stream.ToString() }, + { "Perm|ChangeNickname", role.Permissions.ChangeNickname.ToString() }, + { "Perm|ManageNicknames", role.Permissions.ManageNicknames.ToString() }, + { "Perm|ManageRoles", role.Permissions.ManageRoles.ToString() }, + { "Perm|DeafenMembers", role.Permissions.DeafenMembers.ToString() }, + { "Perm|ManageMessages", role.Permissions.ManageMessages.ToString() }, + { "Perm|ViewChannel", role.Permissions.ViewChannel.ToString() }, + { "Perm|SendMessages", role.Permissions.SendMessages.ToString() }, + { "Perm|CreateInstantInvite", role.Permissions.CreateInstantInvite.ToString() }, + { "Perm|BanMembers", role.Permissions.BanMembers.ToString() }, + { "Perm|SendTTSMessages", role.Permissions.SendTTSMessages.ToString() }, + { "Perm|Administrator", role.Permissions.Administrator.ToString() }, + { "Perm|ManageChannels", role.Permissions.ManageChannels.ToString() }, + { "Perm|KickMembers", role.Permissions.KickMembers.ToString() }, + { "Perm|AddReactions", role.Permissions.AddReactions.ToString() }, + { "Perm|ViewAuditLog", role.Permissions.ViewAuditLog.ToString() }, + { "Perm|ManageWebhooks", role.Permissions.ManageWebhooks.ToString() }, + { "Perm|ManageGuild", role.Permissions.ManageGuild.ToString() }, + { "Perm|ManageEmojis", role.Permissions.ManageEmojis.ToString() } + }; + return new KeyValuePair>(true, settings); + } + } + return new KeyValuePair>(false, new Dictionary()); + } + } +} diff --git a/Core/Commands/DiscordAPI/Role_UpdatePerms.cs b/Core/Commands/DiscordAPI/Role_UpdatePerms.cs new file mode 100644 index 0000000..8847af0 --- /dev/null +++ b/Core/Commands/DiscordAPI/Role_UpdatePerms.cs @@ -0,0 +1,165 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +using OpenMetaverse; +using Newtonsoft.Json; +using Discord.WebSocket; +using Discord; +using Discord.Rest; +using System.Threading.Tasks; + +namespace BSB.Commands.DiscordAPI +{ + class Discord_Role_UpdatePerms : CoreCommand_4arg + { + public override string[] ArgTypes { get { return new[] { "Smart", "Number", "Number","Text" }; } } + public override string[] ArgHints { get { return new[] { "Mixed [Channel|Avatar uuid|Avatar name|http url]", "Discord server id", "Role id","CSV list of PermFlag=Bool" }; } } + public override string Helpfile { get { return "Updates perm flags for the selected role \n example CSV format: Speak=True,SendMessages=False \n for a full list of perms see output of DiscordRoleGetSettings \n This command requires Discord full client mode enabled and connected"; } } + public override bool CallFunction(string[] args) + { + if (base.CallFunction(args) == true) + { + if (bot.discordReady() == false) + { + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], bot.getDiscordWhyFailed(), CommandName); + } + bool status = UpdateRolePerms(args).Result; + return bot.GetCommandsInterface.SmartCommandReply(status, args[0], "see status for update state", CommandName); + } + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], "incorrect args", CommandName); + } + + protected GuildPermissions UpdateFlag(GuildPermissions NewPerms,string flag, bool flagValue) + { + switch (flag) + { + case "CreateInstantInvite": + NewPerms = NewPerms.Modify(flagValue); + break; + case "KickMembers": + NewPerms = NewPerms.Modify(null, flagValue); + break; + case "BanMembers": + NewPerms = NewPerms.Modify(null, null, flagValue); + break; + case "Administrator": + NewPerms = NewPerms.Modify(null, null, null, flagValue); + break; + case "ManageChannels": + NewPerms = NewPerms.Modify(null, null, null, null, flagValue); + break; + case "ManageGuild": + NewPerms = NewPerms.Modify(null, null, null, null, null, flagValue); + break; + case "AddReactions": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, flagValue); + break; + case "ViewAuditLog": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, flagValue); + break; + case "SendTTSMessages": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "AttachFiles": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "ReadMessageHistory": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "MentionEveryone": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "UseExternalEmojis": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "Connect": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "Speak": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "MuteMembers": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "UseVAD": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "MoveMembers": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "EmbedLinks": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "PrioritySpeaker": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "Stream": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "ChangeNickname": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "ManageNicknames": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "ManageRoles": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "DeafenMembers": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "ManageMessages": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "ViewChannel": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, flagValue); + break; + case "SendMessages": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "ManageWebhooks": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + case "ManageEmojis": + NewPerms = NewPerms.Modify(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, flagValue); + break; + default: + break; + } + return NewPerms; + } + + protected async Task UpdateRolePerms(string[] args) + { + DiscordSocketClient Discord = bot.getDiscordClient(); + if (ulong.TryParse(args[1], out ulong serverid) == true) + { + if (ulong.TryParse(args[2], out ulong roleid) == true) + { + SocketGuild server = Discord.GetGuild(serverid); + SocketRole role = server.GetRole(roleid); + string[] bits = args[3].Split(','); + GuildPermissions NewPerms = role.Permissions; + foreach (String bit in bits) + { + string[] subbits = bit.Split('='); + if(subbits.Length == 2) + { + if(bool.TryParse(subbits[1],out bool flagValue) == true) + { + NewPerms = UpdateFlag(NewPerms, subbits[0], flagValue); + } + } + } + await role.ModifyAsync(Rp => + { + Rp.Permissions = NewPerms; + }).ConfigureAwait(true); + return true; + } + } + return false; + } + } +} diff --git a/Core/Commands/DiscordAPI/TextChannels_List.cs b/Core/Commands/DiscordAPI/TextChannels_List.cs new file mode 100644 index 0000000..004e628 --- /dev/null +++ b/Core/Commands/DiscordAPI/TextChannels_List.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +using OpenMetaverse; +using Newtonsoft.Json; +using Discord.WebSocket; +using Discord; +using Discord.Rest; +using System.Threading.Tasks; + +namespace BSB.Commands.DiscordAPI +{ + class Discord_TextChannels_List : CoreCommand_SmartReply_2arg + { + public override string[] ArgTypes { get { return new[] { "Smart", "Number" }; } } + public override string[] ArgHints { get { return new[] { "Mixed [Channel|Avatar uuid|Avatar name|http url]", "Discord server id" }; } } + public override string Helpfile { get { return "Returns a list of text channels in a server \n collection is channelid: name"; } } + public override bool CallFunction(string[] args) + { + if (base.CallFunction(args) == true) + { + if (bot.discordReady() == false) + { + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], bot.getDiscordWhyFailed(), CommandName); + } + KeyValuePair> reply = ListTextChannels(args); + return bot.GetCommandsInterface.SmartCommandReply(reply.Key, args[0], "see status for state", CommandName, reply.Value); + } + return bot.GetCommandsInterface.SmartCommandReply(false, args[0], "incorrect args", CommandName); + } + + protected KeyValuePair> ListTextChannels(string[] args) + { + DiscordSocketClient Discord = bot.getDiscordClient(); + if (ulong.TryParse(args[1], out ulong serverid) == true) + { + SocketGuild server = Discord.GetGuild(serverid); + Dictionary textChannels = new Dictionary(); + foreach(SocketTextChannel channel in server.TextChannels) + { + textChannels.Add(channel.Id.ToString(), channel.Name); + } + return new KeyValuePair>(true, textChannels); + } + return new KeyValuePair>(false, new Dictionary()); + } + } +} diff --git a/Core/bottypes/VirtualsBot.cs b/Core/bottypes/VirtualsBot.cs index 366d0d7..aef9599 100644 --- a/Core/bottypes/VirtualsBot.cs +++ b/Core/bottypes/VirtualsBot.cs @@ -1,17 +1,43 @@ using BetterSecondBotShared.logs; +using Discord.WebSocket; using OpenMetaverse; using System; using System.Collections.Generic; using System.Text; +using Discord; namespace BSB.bottypes { public abstract class VirtualsBot : Log2FileBot { #region DiscordOutputTarget - public virtual bool send_to_discord(string command,string message) - { - return false; + protected DiscordSocketClient DiscordLink = null; + protected string DiscordReadyWhyFailed = ""; + public void setDiscordClient(DiscordSocketClient GivenDiscordLink) + { + DiscordLink = GivenDiscordLink; + } + public DiscordSocketClient getDiscordClient() + { + return DiscordLink; + } + public string getDiscordWhyFailed() + { + return DiscordReadyWhyFailed; + } + public bool discordReady() + { + if (DiscordLink == null) + { + DiscordReadyWhyFailed = "No client"; + return false; + } + if (DiscordLink.ConnectionState != ConnectionState.Connected) + { + DiscordReadyWhyFailed = "Not connected"; + return false; + } + return true; } #endregion public virtual Dictionary GetIMChatWindowKeyNames()