├── .gitattributes ├── .gitignore ├── DiscordExample.sln ├── DiscordExample ├── App.config ├── DiscordExample.csproj ├── PermissionLevels.cs ├── Program.cs ├── Properties │ └── AssemblyInfo.cs ├── StandardModule.cs └── packages.config └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | 28 | # MSTest test Results 29 | [Tt]est[Rr]esult*/ 30 | [Bb]uild[Ll]og.* 31 | 32 | # NUNIT 33 | *.VisualState.xml 34 | TestResult.xml 35 | 36 | # Build Results of an ATL Project 37 | [Dd]ebugPS/ 38 | [Rr]eleasePS/ 39 | dlldata.c 40 | 41 | # DNX 42 | project.lock.json 43 | artifacts/ 44 | 45 | *_i.c 46 | *_p.c 47 | *_i.h 48 | *.ilk 49 | *.meta 50 | *.obj 51 | *.pch 52 | *.pdb 53 | *.pgc 54 | *.pgd 55 | *.rsp 56 | *.sbr 57 | *.tlb 58 | *.tli 59 | *.tlh 60 | *.tmp 61 | *.tmp_proj 62 | *.log 63 | *.vspscc 64 | *.vssscc 65 | .builds 66 | *.pidb 67 | *.svclog 68 | *.scc 69 | 70 | # Chutzpah Test files 71 | _Chutzpah* 72 | 73 | # Visual C++ cache files 74 | ipch/ 75 | *.aps 76 | *.ncb 77 | *.opensdf 78 | *.sdf 79 | *.cachefile 80 | 81 | # Visual Studio profiler 82 | *.psess 83 | *.vsp 84 | *.vspx 85 | 86 | # TFS 2012 Local Workspace 87 | $tf/ 88 | 89 | # Guidance Automation Toolkit 90 | *.gpState 91 | 92 | # ReSharper is a .NET coding add-in 93 | _ReSharper*/ 94 | *.[Rr]e[Ss]harper 95 | *.DotSettings.user 96 | 97 | # JustCode is a .NET coding add-in 98 | .JustCode 99 | 100 | # TeamCity is a build add-in 101 | _TeamCity* 102 | 103 | # DotCover is a Code Coverage Tool 104 | *.dotCover 105 | 106 | # NCrunch 107 | _NCrunch_* 108 | .*crunch*.local.xml 109 | 110 | # MightyMoose 111 | *.mm.* 112 | AutoTest.Net/ 113 | 114 | # Web workbench (sass) 115 | .sass-cache/ 116 | 117 | # Installshield output folder 118 | [Ee]xpress/ 119 | 120 | # DocProject is a documentation generator add-in 121 | DocProject/buildhelp/ 122 | DocProject/Help/*.HxT 123 | DocProject/Help/*.HxC 124 | DocProject/Help/*.hhc 125 | DocProject/Help/*.hhk 126 | DocProject/Help/*.hhp 127 | DocProject/Help/Html2 128 | DocProject/Help/html 129 | 130 | # Click-Once directory 131 | publish/ 132 | 133 | # Publish Web Output 134 | *.[Pp]ublish.xml 135 | *.azurePubxml 136 | ## TODO: Comment the next line if you want to checkin your 137 | ## web deploy settings but do note that will include unencrypted 138 | ## passwords 139 | #*.pubxml 140 | 141 | *.publishproj 142 | 143 | # NuGet Packages 144 | *.nupkg 145 | # The packages folder can be ignored because of Package Restore 146 | **/packages/* 147 | # except build/, which is used as an MSBuild target. 148 | !**/packages/build/ 149 | # Uncomment if necessary however generally it will be regenerated when needed 150 | #!**/packages/repositories.config 151 | 152 | # Windows Azure Build Output 153 | csx/ 154 | *.build.csdef 155 | 156 | # Windows Store app package directory 157 | AppPackages/ 158 | 159 | # Visual Studio cache files 160 | # files ending in .cache can be ignored 161 | *.[Cc]ache 162 | # but keep track of directories ending in .cache 163 | !*.[Cc]ache/ 164 | 165 | # Others 166 | ClientBin/ 167 | [Ss]tyle[Cc]op.* 168 | ~$* 169 | *~ 170 | *.dbmdl 171 | *.dbproj.schemaview 172 | *.pfx 173 | *.publishsettings 174 | node_modules/ 175 | orleans.codegen.cs 176 | 177 | # RIA/Silverlight projects 178 | Generated_Code/ 179 | 180 | # Backup & report files from converting an old project file 181 | # to a newer Visual Studio version. Backup files are not needed, 182 | # because we have git ;-) 183 | _UpgradeReport_Files/ 184 | Backup*/ 185 | UpgradeLog*.XML 186 | UpgradeLog*.htm 187 | 188 | # SQL Server files 189 | *.mdf 190 | *.ldf 191 | 192 | # Business Intelligence projects 193 | *.rdl.data 194 | *.bim.layout 195 | *.bim_*.settings 196 | 197 | # Microsoft Fakes 198 | FakesAssemblies/ 199 | 200 | # Node.js Tools for Visual Studio 201 | .ntvs_analysis.dat 202 | 203 | # Visual Studio 6 build log 204 | *.plg 205 | 206 | # Visual Studio 6 workspace options file 207 | *.opt 208 | 209 | # LightSwitch generated files 210 | GeneratedArtifacts/ 211 | _Pvt_Extensions/ 212 | ModelManifest.xml 213 | -------------------------------------------------------------------------------- /DiscordExample.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.24720.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiscordExample", "DiscordExample\DiscordExample.csproj", "{B56D1DBE-7B4E-4456-8DCB-0E66456341A5}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {B56D1DBE-7B4E-4456-8DCB-0E66456341A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {B56D1DBE-7B4E-4456-8DCB-0E66456341A5}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {B56D1DBE-7B4E-4456-8DCB-0E66456341A5}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {B56D1DBE-7B4E-4456-8DCB-0E66456341A5}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /DiscordExample/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /DiscordExample/DiscordExample.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {B56D1DBE-7B4E-4456-8DCB-0E66456341A5} 8 | Exe 9 | Properties 10 | DiscordExample 11 | DiscordExample 12 | v4.5.2 13 | 512 14 | true 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | False 38 | ..\..\..\Discord.Net\src\Discord.Net.Commands.Net45\bin\Debug\Discord.Net.dll 39 | 40 | 41 | False 42 | ..\..\..\Discord.Net\src\Discord.Net.Commands.Net45\bin\Debug\Discord.Net.Commands.dll 43 | 44 | 45 | False 46 | ..\..\..\Discord.Net\src\Discord.Net.Modules.Net45\bin\Debug\Discord.Net.Modules.dll 47 | 48 | 49 | ..\packages\Newtonsoft.Json.8.0.1\lib\net45\Newtonsoft.Json.dll 50 | True 51 | 52 | 53 | ..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.dll 54 | True 55 | 56 | 57 | ..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Concurrent.dll 58 | True 59 | 60 | 61 | ..\packages\Nito.AsyncEx.3.0.1\lib\net45\Nito.AsyncEx.Enlightenment.dll 62 | True 63 | 64 | 65 | ..\packages\RestSharp.105.2.3\lib\net452\RestSharp.dll 66 | True 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | ..\packages\WebSocket4Net.0.14.1\lib\net45\WebSocket4Net.dll 78 | True 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 99 | -------------------------------------------------------------------------------- /DiscordExample/PermissionLevels.cs: -------------------------------------------------------------------------------- 1 | namespace DiscordExample 2 | { 3 | public enum PermissionLevel 4 | { 5 | Ignored = -1, 6 | User = 0, 7 | ChannelModerator, //Manage Messages (Channel) 8 | ChannelAdmin, //Manage Permissions (Channel) 9 | ServerModerator, //Manage Messages, Kick, Ban (Server) 10 | ServerAdmin, //Manage Roles (Server) 11 | ServerOwner, //Owner (Server) 12 | BotOwner //Bot Owner (Global) 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /DiscordExample/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Text.RegularExpressions; 7 | using System.Threading.Tasks; 8 | using Discord; 9 | using Discord.Commands; 10 | using Discord.Commands.Permissions.Levels; 11 | using Discord.Modules; 12 | using Newtonsoft.Json; 13 | 14 | namespace DiscordExample 15 | { 16 | class Program 17 | { 18 | public static void Main(string[] args) => new Program().Start(args); 19 | 20 | private const string AppName = "DiscordExample"; // Change this to the name of your bot 21 | 22 | private DiscordClient _client; 23 | 24 | private void Start(string[] args) 25 | { 26 | _client = new DiscordClient(x => 27 | { 28 | x.AppName = AppName; 29 | x.MessageCacheSize = 10; 30 | x.EnablePreUpdateEvents = true; 31 | }) 32 | .UsingCommands(x => 33 | { 34 | x.AllowMentionPrefix = true; 35 | // x.PrefixChar = '!'; 36 | // Please don't use !, there's a million bots that already do. 37 | x.HelpMode = HelpMode.Public; 38 | x.ExecuteHandler += (s, e) => _client.Log.Info("Command", $"[{((e.Server != null) ? e.Server.Name : "Private")}{((!e.Channel.IsPrivate) ? $"/#{e.Channel.Name}" : "")}] <@{e.User.Name}> {e.Command.Text} {((e.Args.Length > 0) ? "| " + string.Join(" ", e.Args) : "")}"); 39 | x.ErrorHandler = CommandError; 40 | }) 41 | .UsingPermissionLevels((u, c) => (int)GetPermissions(u, c)) 42 | .UsingModules(); 43 | 44 | _client.Log.Message += (s, e) => WriteLog(e); 45 | _client.MessageReceived += (s, e) => 46 | { 47 | if (e.Message.IsAuthor) 48 | _client.Log.Info("< {e.Message.Text}"); 49 | else 50 | _client.Log.Info(">>Message", $"[{((e.Server != null) ? e.Server.Name : "Private")}{((!e.Channel.IsPrivate) ? $"/#{e.Channel.Name}" : "")}] <@{e.User.Name}> {e.Message.Text}"); 51 | }; 52 | 53 | _client.AddModule("Standard", ModuleFilter.None); 54 | 55 | _client.ExecuteAndWait(async () => 56 | { 57 | await _client.Connect("token"); 58 | 59 | _client.Log.Info("Connected", $"Connected as {_client.CurrentUser.Name} (Id {_client.CurrentUser.Id})"); 60 | }); 61 | } 62 | 63 | private static PermissionLevel GetPermissions(User u, Channel c) 64 | { 65 | if (u.Id == 102528327251656704) // Replace this with your own UserId 66 | return PermissionLevel.BotOwner; 67 | 68 | if (u.IsBot) // Customize this to your liking to ignore other stuff, like a list of known spammers. 69 | { 70 | return PermissionLevel.Ignored; 71 | } 72 | 73 | if (!c.IsPrivate) 74 | { 75 | if (u == c.Server.Owner) 76 | return PermissionLevel.ServerOwner; 77 | 78 | var serverPerms = u.ServerPermissions; 79 | if (serverPerms.ManageRoles || u.Roles.Select(x => x.Name.ToLower()).Contains("bot commander")) 80 | return PermissionLevel.ServerAdmin; 81 | if (serverPerms.ManageMessages && serverPerms.KickMembers && serverPerms.BanMembers) 82 | return PermissionLevel.ServerModerator; 83 | 84 | var channelPerms = u.GetPermissions(c); 85 | if (channelPerms.ManagePermissions) 86 | return PermissionLevel.ChannelAdmin; 87 | if (channelPerms.ManageMessages) 88 | return PermissionLevel.ChannelModerator; 89 | } 90 | return PermissionLevel.User; 91 | } 92 | 93 | private async void CommandError(object sender, CommandErrorEventArgs e) 94 | { 95 | if (e.ErrorType == CommandErrorType.Exception) 96 | { 97 | _client.Log.Error("Command", e.Exception); 98 | await e.Channel.SendMessage($"Error: {e.Exception.GetBaseException().Message}"); 99 | } 100 | else if (e.ErrorType == CommandErrorType.BadPermissions) 101 | { 102 | if (e.Exception?.Message == "This module is currently disabled.") 103 | { 104 | await e.Channel.SendMessage($"The `{e.Command?.Category}` module is currently disabled."); 105 | return; 106 | } 107 | else if (e.Exception != null) 108 | { 109 | await e.Channel.SendMessage(e.Exception.Message); 110 | return; 111 | } 112 | 113 | if (e.Command?.IsHidden == true) 114 | return; 115 | 116 | await e.Channel.SendMessage($"You don't have permission to access that command!"); 117 | } 118 | else if (e.ErrorType == CommandErrorType.BadArgCount) 119 | { 120 | await e.Channel.SendMessage("Error: Invalid parameter count."); 121 | } 122 | else if (e.ErrorType == CommandErrorType.InvalidInput) 123 | { 124 | await e.Channel.SendMessage("Error: Invalid input! Make sure your quotes match up correctly!"); 125 | } 126 | else if (e.ErrorType == CommandErrorType.UnknownCommand) 127 | { 128 | // Only set up a response in here if you stick with a mention prefix 129 | } 130 | } 131 | 132 | private void WriteLog(LogMessageEventArgs e) 133 | { 134 | //Color 135 | ConsoleColor color; 136 | switch (e.Severity) 137 | { 138 | case LogSeverity.Error: color = ConsoleColor.Red; break; 139 | case LogSeverity.Warning: color = ConsoleColor.Yellow; break; 140 | case LogSeverity.Info: color = ConsoleColor.White; break; 141 | case LogSeverity.Verbose: color = ConsoleColor.Gray; break; 142 | case LogSeverity.Debug: default: color = ConsoleColor.DarkGray; break; 143 | } 144 | 145 | //Exception 146 | string exMessage; 147 | Exception ex = e.Exception; 148 | if (ex != null) 149 | { 150 | while (ex is AggregateException && ex.InnerException != null) 151 | ex = ex.InnerException; 152 | exMessage = $"{ex.Message}"; 153 | if (exMessage != "Reconnect failed: HTTP/1.1 503 Service Unavailable") 154 | exMessage += $"\n{ex.StackTrace}"; 155 | } 156 | else 157 | exMessage = null; 158 | 159 | //Source 160 | string sourceName = e.Source?.ToString(); 161 | 162 | //Text 163 | string text; 164 | if (e.Message == null) 165 | { 166 | text = exMessage ?? ""; 167 | exMessage = null; 168 | } 169 | else 170 | text = e.Message; 171 | 172 | if (sourceName == "Command") 173 | color = ConsoleColor.Cyan; 174 | else if (sourceName == "< 24 | { 25 | cgb.MinPermissions((int)PermissionLevel.User); 26 | 27 | cgb.CreateCommand("say") 28 | .MinPermissions((int)PermissionLevel.BotOwner) // An unrestricted say command is a bad idea 29 | .Description("Make the bot speak!") 30 | .Parameter("text", ParameterType.Unparsed) 31 | .Do(async e => 32 | { 33 | await e.Channel.SendMessage(e.GetArg("text")); 34 | }); 35 | 36 | cgb.CreateCommand("params") // This command doesn't have any use, just an example for ParamenterType.Multiple 37 | .Description("Multiple paramter test") 38 | .Parameter("text", ParameterType.Multiple) 39 | .Do(async e => 40 | { 41 | StringBuilder output = new StringBuilder(); 42 | 43 | output.AppendLine("Parameters:"); 44 | 45 | for (int i = 0; i < e.Args.Length; i++) 46 | { 47 | output.AppendLine($"{i}: {e.Args[i]}"); 48 | } 49 | 50 | await e.Channel.SendMessage(output.ToString()); 51 | }); 52 | }); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /DiscordExample/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DiscordExample 2 | Also known as "GoogieBot" 3 | 4 | This bot is a nice starting point for a Discord.Net bot, setting up a basic module with a couple simple commands. 5 | It has console output already set up, as well as command error responses. 6 | 7 | Have something you'd like to add to it? Send a PR and I'll add it in. 8 | --------------------------------------------------------------------------------