diff --git a/src/.vs/Discord.json/DesignTimeBuild/.dtbcache b/src/.vs/Discord.json/DesignTimeBuild/.dtbcache index 1479f27..1145771 100644 Binary files a/src/.vs/Discord.json/DesignTimeBuild/.dtbcache and b/src/.vs/Discord.json/DesignTimeBuild/.dtbcache differ diff --git a/src/.vs/Discord.json/v15/.suo b/src/.vs/Discord.json/v15/.suo index 123daf4..9a9d693 100644 Binary files a/src/.vs/Discord.json/v15/.suo and b/src/.vs/Discord.json/v15/.suo differ diff --git a/src/.vs/Discord.json/v15/Server/sqlite3/storage.ide b/src/.vs/Discord.json/v15/Server/sqlite3/storage.ide index bbf670f..9631482 100644 Binary files a/src/.vs/Discord.json/v15/Server/sqlite3/storage.ide and b/src/.vs/Discord.json/v15/Server/sqlite3/storage.ide differ diff --git a/src/.vs/Discord.json/v15/Server/sqlite3/storage.ide-shm b/src/.vs/Discord.json/v15/Server/sqlite3/storage.ide-shm index 334aaec..8277803 100644 Binary files a/src/.vs/Discord.json/v15/Server/sqlite3/storage.ide-shm and b/src/.vs/Discord.json/v15/Server/sqlite3/storage.ide-shm differ diff --git a/src/.vs/Discord.json/v15/Server/sqlite3/storage.ide-wal b/src/.vs/Discord.json/v15/Server/sqlite3/storage.ide-wal index b99dd82..841800c 100644 Binary files a/src/.vs/Discord.json/v15/Server/sqlite3/storage.ide-wal and b/src/.vs/Discord.json/v15/Server/sqlite3/storage.ide-wal differ diff --git a/src/Actions.cs b/src/Actions.cs index 8ddb11d..b4cf31c 100644 --- a/src/Actions.cs +++ b/src/Actions.cs @@ -12,7 +12,7 @@ namespace Discord.json public class Actions { // Holds information about the guild, message, user, bot etc - public static SocketCommandContext Context { get; set; } + public static ICommandContext Context { get; set; } // This is a list of all vaild jactions public Dictionary JActions { get; set; } @@ -76,7 +76,7 @@ public async Task MoveChannel(SocketTextChannel channel, string categoryName) { try { - var category = Context.Guild.CategoryChannels.ToList().Find(c => c.Name == categoryName); + var category = (Context.Guild as SocketGuild).CategoryChannels.ToList().Find(c => c.Name == categoryName); await channel.ModifyAsync(x => x.CategoryId = category.Id); } @@ -91,10 +91,10 @@ public async Task ChangeServerName(string newName) { try { - await Context.Guild.ModifyAsync(async x => - { - x.Name = newName; - }); + await Context.Guild.ModifyAsync(x => + { + x.Name = newName; + }); } catch (Exception e) @@ -109,33 +109,37 @@ public async Task ExecuteAsync(SocketCommandContext context, JsonCommand command foreach (var action in command.Actions) { - var localArgs = ArgParser(action.Arguments, cmdData); + var args = ArgParser(action.Arguments, cmdData.Arguments); + await ExecuteActionAsync(action, args); + } + } - var commandMethod = JActions[action.Name.ToLower()]; - try + public async Task ExecuteActionAsync(JsonAction action, List localArgs) + { + var commandMethod = JActions[action.Name.ToLower()]; + try + { + // Checks if the parameters are the correct legnth to avoid crashes + if (localArgs.Count > commandMethod.Parameters.Count) { - // Checks if the parameters are the correct legnth to avoid crashes - if (localArgs.Count > commandMethod.Parameters.Count) - { - // Gets the amount of extra paramerts then removes those from the end of the list - var amount = localArgs.Count - commandMethod.Parameters.Count; - localArgs.RemoveRange(commandMethod.Parameters.Count, amount); - } - else if (localArgs.Count < commandMethod.Parameters.Count) - { - await ReplyAsync("Not Enough Parameters"); - } - - await ReflectionHelper.InvokeMethod(new Actions(), commandMethod.Name, localArgs.ToArray()); + // Gets the amount of extra paramerts then removes those from the end of the list + var amount = localArgs.Count - commandMethod.Parameters.Count; + localArgs.RemoveRange(commandMethod.Parameters.Count, amount); } - catch (Exception e) + else if (localArgs.Count < commandMethod.Parameters.Count) { - Console.WriteLine(e); + await ReplyAsync("Not Enough Parameters"); } + + await ReflectionHelper.InvokeMethod(new Actions(), commandMethod.Name, localArgs.ToArray()); + } + catch (Exception e) + { + Console.WriteLine(e); } } - private List ArgParser(List actionArgs, CommandArgs userArgs) + public List ArgParser(List actionArgs, List userArgs) { var returnArgs = new List(); @@ -147,7 +151,7 @@ private List ArgParser(List actionArgs, CommandArgs userArgs) var bind = new Regex(@"\|(.*?)\|"); // This pattern matches everything between || var bindMatches = bind.Matches(_arg); - var mention = new Regex("^<[#@&]+?([0-9]+?)>$"); + var mention = new Regex("^<[#@&!]+?([0-9]+?)>$"); // Checks if there is any matches // If there is go through each one and replace it with the correct information @@ -163,35 +167,21 @@ private List ArgParser(List actionArgs, CommandArgs userArgs) if (Int32.TryParse(arg, out int index)) { - var posArg = userArgs.Arguments[index]; + var posArg = userArgs[index]; returnArg = (returnArg as string).Replace(match.Value, posArg.ToString()); var mentionMatch = mention.Match((string)returnArg); - if (mentionMatch.Success) - { - var mUlong = mentionMatch.Groups[1].Value; - var mMatch = mentionMatch.Value; - - UInt64.TryParse(mUlong, out ulong id); - if (mMatch.StartsWith("<@")) - { - returnArg = Context.Guild.GetUser(id); - } - else if (mMatch.StartsWith("<#")) - { - returnArg = Context.Guild.GetTextChannel(id); - } - /*else if (mMatch.Contains("<@&")) // Allows for @[role] in the args to be used as SocketRole - { - returnArg = Context.Guild.GetRole(id); - }*/ - } + returnArg = ReplaceMentions(mentionMatch); } else { var bindArg = ReflectionHelper.GetPropValue(this, JPropertyBinds.Binds[arg]); returnArg = (returnArg as string).Replace(match.Value, bindArg); + + var mentionMatch = mention.Match((string)returnArg); + if (mentionMatch.Success) + returnArg = ReplaceMentions(mentionMatch); } } returnArgs.Add(returnArg); @@ -202,5 +192,27 @@ private List ArgParser(List actionArgs, CommandArgs userArgs) } return returnArgs; } + + object ReplaceMentions(Match match) + { + object returnArg = null; + var mUlong = match.Groups[1].Value; + var mMatch = match.Value; + + UInt64.TryParse(mUlong, out ulong id); + if (mMatch.StartsWith("<@")) + { + returnArg = (Context.Guild as SocketGuild).GetUser(id); + } + else if (mMatch.StartsWith("<#")) + { + returnArg = (Context.Guild as SocketGuild).GetTextChannel(id); + } + /*else if (mMatch.Contains("<@&")) // Allows for @[role] in the args to be used as SocketRole + { + returnArg = Context.Guild.GetRole(id); + }*/ + return returnArg; + } } } diff --git a/src/JBot.cs b/src/JBot.cs index 44e8c8a..1bedb72 100644 --- a/src/JBot.cs +++ b/src/JBot.cs @@ -97,6 +97,7 @@ private async Task RunBotAsync() Client.MessageReceived += HandleCommandAsync; RegisterCommands(); + RegisterEvents(); // Log in as bot try @@ -140,6 +141,39 @@ private void RegisterCommands() } } + private void RegisterEvents() + { + if (_data.Events.Find(e => e.Name == "UserJoined") != null) + Client.UserJoined += ExecuteEvent; + } + + public async Task ExecuteEvent(object args) + { + var eventObjects = new Dictionary + { + { typeof(SocketGuildUser), "UserJoined" } + }; + await ExecuteActionsAsync(eventObjects[args.GetType()], args); + } + + private async Task ExecuteActionsAsync(string eventName, object args) + { + var eventData = _data.Events.Find(e => e.Name == eventName); + switch (eventName) + { + case "UserJoined": + + foreach (var action in eventData.Actions) + { + var user = (args as SocketGuildUser); + Actions.Context = new SocketEventContext(Client, user); + var argList = _actions.ArgParser(action.Arguments, new List());//new List(action.Arguments); + await _actions.ExecuteActionAsync(action, argList); + } + break; + } + } + private Task Log(LogMessage arg) { if (PrintLog) @@ -151,7 +185,7 @@ private Task Log(LogMessage arg) private async Task HandleCommandAsync(SocketMessage arg) { if (!(arg is SocketUserMessage message) || message.Author.IsBot) return; - Console.WriteLine(message.Content); + int argPos = 0; if (message.HasStringPrefix(Prefix, ref argPos) || message.HasMentionPrefix(Client.CurrentUser, ref argPos) && AllowMentionPrefix) { @@ -190,4 +224,4 @@ public JsonCommand GetCommand(string command) } } } -} +} \ No newline at end of file diff --git a/src/Program.cs b/src/Program.cs index 6df5ca3..f7d0007 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -1,5 +1,4 @@ using Newtonsoft.Json; -using System; using System.IO; using System.Linq; diff --git a/src/SocketEventContext.cs b/src/SocketEventContext.cs new file mode 100644 index 0000000..0a8d929 --- /dev/null +++ b/src/SocketEventContext.cs @@ -0,0 +1,41 @@ +using Discord.Commands; +using Discord.WebSocket; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Discord.json +{ + public class SocketEventContext : ICommandContext + { + public DiscordSocketClient Client { get; } + public SocketGuild Guild { get; } + public ISocketMessageChannel Channel { get; } + public SocketUser User { get; } + public SocketUserMessage Message { get; } + + public bool IsPrivate => Channel is IPrivateChannel; + + public SocketEventContext(DiscordSocketClient client, SocketGuild guild) + { + Client = client; + Guild = guild; + Channel = guild.DefaultChannel; + } + + public SocketEventContext(DiscordSocketClient client, SocketGuildUser user) + { + Client = client; + Guild = user.Guild; + Channel = user.Guild.DefaultChannel; + User = user; + } + + //ICommandContext + IDiscordClient ICommandContext.Client => Client; + IGuild ICommandContext.Guild => Guild; + IMessageChannel ICommandContext.Channel => Channel; + IUser ICommandContext.User => User; + IUserMessage ICommandContext.Message => Message; + } +} diff --git a/src/bot/main.bot b/src/bot/main.bot index 593a6e2..d9b828e 100644 --- a/src/bot/main.bot +++ b/src/bot/main.bot @@ -24,6 +24,13 @@ "actions": [ { "action": "DeleteRole", "args": [ "|0|" ]} ] + }, + { + "name": "me", + "args": 0, + "actions": [ + { "action": "Reply", "args": [ "|user|" ]} + ] } ], "events": [ @@ -32,7 +39,11 @@ "actions": [ { "action": "Reply", - "args": [ "Hello |user| welcome to |guild|" ] + "args": [ "Hello |user-name| welcome to |guild|" ] + }, + { + "action": "AddRole", + "args": [ "|user|", "red" ] } ] } diff --git a/src/bot/properties.binds b/src/bot/properties.binds index 596561c..31ee8a4 100644 --- a/src/bot/properties.binds +++ b/src/bot/properties.binds @@ -1,4 +1,5 @@ { - "user": "Context.User.Username", + "user-name": "Context.User.Username", + "user": "Context.User.Mention", "guild": "Context.Guild.Name" } \ No newline at end of file diff --git a/src/obj/Debug/netcoreapp2.1/Discord.json.assets.cache b/src/obj/Debug/netcoreapp2.1/Discord.json.assets.cache index b70e25d..d84f893 100644 Binary files a/src/obj/Debug/netcoreapp2.1/Discord.json.assets.cache and b/src/obj/Debug/netcoreapp2.1/Discord.json.assets.cache differ diff --git a/src/obj/Debug/netcoreapp2.1/Discord.json.csproj.CoreCompileInputs.cache b/src/obj/Debug/netcoreapp2.1/Discord.json.csproj.CoreCompileInputs.cache index 33b5b7a..5ab5535 100644 --- a/src/obj/Debug/netcoreapp2.1/Discord.json.csproj.CoreCompileInputs.cache +++ b/src/obj/Debug/netcoreapp2.1/Discord.json.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -8b9b3faa1965784bd980f396bcac7ebac9f7989c +9a297fc2bf746ff33ff76604dbf5b235bf418e75 diff --git a/src/obj/Debug/netcoreapp2.1/Discord.json.csprojAssemblyReference.cache b/src/obj/Debug/netcoreapp2.1/Discord.json.csprojAssemblyReference.cache index dc9953d..1357f10 100644 Binary files a/src/obj/Debug/netcoreapp2.1/Discord.json.csprojAssemblyReference.cache and b/src/obj/Debug/netcoreapp2.1/Discord.json.csprojAssemblyReference.cache differ diff --git a/src/obj/Debug/netcoreapp2.1/Discord.json.dll b/src/obj/Debug/netcoreapp2.1/Discord.json.dll index b75b8e4..fdf8ce5 100644 Binary files a/src/obj/Debug/netcoreapp2.1/Discord.json.dll and b/src/obj/Debug/netcoreapp2.1/Discord.json.dll differ diff --git a/src/obj/Debug/netcoreapp2.1/Discord.json.pdb b/src/obj/Debug/netcoreapp2.1/Discord.json.pdb index 3f7f482..994e0ce 100644 Binary files a/src/obj/Debug/netcoreapp2.1/Discord.json.pdb and b/src/obj/Debug/netcoreapp2.1/Discord.json.pdb differ diff --git a/src/obj/Discord.json.csproj.nuget.cache b/src/obj/Discord.json.csproj.nuget.cache index 15f6c2e..879d67e 100644 --- a/src/obj/Discord.json.csproj.nuget.cache +++ b/src/obj/Discord.json.csproj.nuget.cache @@ -1,5 +1,5 @@ { "version": 1, - "dgSpecHash": "Wym/vBQUn0ukCzyHlvYd/HRcIgzsYJGAUx+8A03czho3gyGOiSWjAdFSQmQGL66MOJr/Y2BoSp8OjMvTbXU7IQ==", + "dgSpecHash": "nH5wfO3r7TR9jL7bFyRNdbmHI7iYdvX8mDWQ0SxnTL/RiFYzY6K/Ulp7vxwI/eo20KBQE2Z5JcCc8QezgtKfRA==", "success": true } \ No newline at end of file diff --git a/src/obj/Discord.json.csproj.nuget.g.props b/src/obj/Discord.json.csproj.nuget.g.props index c8bd6ee..4bb022b 100644 --- a/src/obj/Discord.json.csproj.nuget.g.props +++ b/src/obj/Discord.json.csproj.nuget.g.props @@ -3,11 +3,11 @@ True NuGet - C:\Users\dotcd\Desktop\Desktop\Git\Discord.json\src\obj\project.assets.json + C:\Users\TGN-PC\Documents\Visual Studio 2017\Projects\EzBot\EzBot.Json\src\obj\project.assets.json $(UserProfile)\.nuget\packages\ - C:\Users\dotcd\.nuget\packages\;C:\Program Files\dotnet\sdk\NuGetFallbackFolder + C:\Users\TGN-PC\.nuget\packages\;C:\Program Files\dotnet\sdk\NuGetFallbackFolder PackageReference - 4.8.1 + 4.7.0 $(MSBuildAllProjects);$(MSBuildThisFileFullPath) diff --git a/src/obj/project.assets.json b/src/obj/project.assets.json index 253b39c..3b9b395 100644 --- a/src/obj/project.assets.json +++ b/src/obj/project.assets.json @@ -5891,23 +5891,23 @@ ] }, "packageFolders": { - "C:\\Users\\dotcd\\.nuget\\packages\\": {}, + "C:\\Users\\TGN-PC\\.nuget\\packages\\": {}, "C:\\Program Files\\dotnet\\sdk\\NuGetFallbackFolder": {} }, "project": { "version": "1.0.0", "restore": { - "projectUniqueName": "C:\\Users\\dotcd\\Desktop\\Desktop\\Git\\Discord.json\\src\\Discord.json.csproj", + "projectUniqueName": "C:\\Users\\TGN-PC\\Documents\\Visual Studio 2017\\Projects\\EzBot\\EzBot.Json\\src\\Discord.json.csproj", "projectName": "Discord.json", - "projectPath": "C:\\Users\\dotcd\\Desktop\\Desktop\\Git\\Discord.json\\src\\Discord.json.csproj", - "packagesPath": "C:\\Users\\dotcd\\.nuget\\packages\\", - "outputPath": "C:\\Users\\dotcd\\Desktop\\Desktop\\Git\\Discord.json\\src\\obj\\", + "projectPath": "C:\\Users\\TGN-PC\\Documents\\Visual Studio 2017\\Projects\\EzBot\\EzBot.Json\\src\\Discord.json.csproj", + "packagesPath": "C:\\Users\\TGN-PC\\.nuget\\packages\\", + "outputPath": "C:\\Users\\TGN-PC\\Documents\\Visual Studio 2017\\Projects\\EzBot\\EzBot.Json\\src\\obj\\", "projectStyle": "PackageReference", "fallbackFolders": [ "C:\\Program Files\\dotnet\\sdk\\NuGetFallbackFolder" ], "configFilePaths": [ - "C:\\Users\\dotcd\\AppData\\Roaming\\NuGet\\NuGet.Config", + "C:\\Users\\TGN-PC\\AppData\\Roaming\\NuGet\\NuGet.Config", "C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config" ], "originalTargetFrameworks": [