Skip to content

Commit 9a518c9

Browse files
committed
add guild id in json configuration
1 parent b71e688 commit 9a518c9

File tree

4 files changed

+15
-58
lines changed

4 files changed

+15
-58
lines changed

CommandHandler.cs

+1-37
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@ public class CommandHandler
1515
private readonly IServiceProvider _serviceProvider;
1616
private InteractionService _interactionService { get; set; }
1717

18-
private char Prefix;
19-
2018
// Retrieve client and CommandService instance via vector
21-
public CommandHandler(DiscordSocketClient client, CommandService commands, IServiceProvider serviceProvider, char prefix)
19+
public CommandHandler(DiscordSocketClient client, CommandService commands, IServiceProvider serviceProvider)
2220
{
2321
InteractionServiceConfig _interationServiceConfig = new InteractionServiceConfig();
2422
_interationServiceConfig.EnableAutocompleteHandlers = true;
2523
_interactionService = new(_client, config: _interationServiceConfig);
2624
_commands = commands;
2725
_client = client;
2826
_serviceProvider = serviceProvider;
29-
Prefix = prefix;
3027
}
3128

3229
/// <summary>
@@ -36,7 +33,6 @@ public CommandHandler(DiscordSocketClient client, CommandService commands, IServ
3633
public async Task InstallCommandsAsync()
3734
{
3835
// Hook the MessageReceived event into our command handler
39-
_client.MessageReceived += HandleCommandAsync;
4036
_client.AutocompleteExecuted += AutocompleteExecuted;
4137
_client.SlashCommandExecuted += SlashCommandHandler;
4238
_client.InteractionCreated += InteractionCreated;
@@ -69,38 +65,6 @@ private async Task InteractionCreated(SocketInteraction interaction)
6965
}
7066
}
7167

72-
73-
/// <summary>
74-
/// Handle the command execution
75-
/// </summary>
76-
/// <param name="messageParam"></param>
77-
/// <returns></returns>
78-
private async Task HandleCommandAsync(SocketMessage messageParam)
79-
{
80-
// Don't process the command if it was a system message
81-
var message = messageParam as SocketUserMessage;
82-
if (message == null) return;
83-
84-
// Create a number to track where the prefix ends and the command begins
85-
int argPos = 0;
86-
87-
// Determine if the message is a command based on the prefix and make sure no bots trigger commands
88-
if (!(message.HasCharPrefix(Prefix, ref argPos) ||
89-
message.HasMentionPrefix(_client.CurrentUser, ref argPos)) ||
90-
message.Author.IsBot)
91-
return;
92-
93-
// Create a WebSocket-based command context based on the message
94-
var context = new SocketCommandContext(_client, message);
95-
96-
// Execute the command with the command context we just
97-
// created, along with the service provider for precondition checks.
98-
await _commands.ExecuteAsync(
99-
context: context,
100-
argPos: argPos,
101-
services: _serviceProvider);
102-
}
103-
10468
/// <summary>
10569
/// Slash command handler
10670
/// </summary>

Configuration/Class/BotConfiguration.cs

+4-18
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,20 @@ namespace AutoCompleteBot.Configuration.Class
99
{
1010
public class BotConfiguration
1111
{
12-
public string Language {set; get;}
13-
public char Prefix {set; get;}
1412
public bool DevMode {set; get;}
1513
public string Discord_Link { set; get; } = string.Empty;
14+
public ulong Guild_Id {set;get;}
1615

17-
private BotConfiguration() { Language = string.Empty;Prefix = '!';DevMode = false; }
16+
private BotConfiguration() { DevMode = false; }
1817

1918
private static readonly Lazy<BotConfiguration> instance = new Lazy<BotConfiguration>(() => new BotConfiguration());
2019
public static BotConfiguration Instance { get { return instance.Value; } }
2120
public bool IsDevMode { get { return DevMode; } }
22-
public void InitializeBot(bool devMode, string discord_link)
21+
public void InitializeBot(bool devMode, string discord_link, ulong guild_id)
2322
{
24-
// Get the Bot default configuration
25-
var botCfgTemp = JsonConvert.DeserializeObject<BotConfiguration>(File.ReadAllText("Configuration/botCfg.json"));
26-
27-
if (botCfgTemp != null)
28-
{
29-
Language = botCfgTemp.Language;
30-
Prefix = botCfgTemp.Prefix;
31-
}
32-
else
33-
{
34-
Language = "en";
35-
Prefix = '!';
36-
}
37-
3823
Discord_Link = discord_link;
3924
DevMode = devMode;
25+
Guild_Id = guild_id;
4026
}
4127

4228
}

Configuration/discordCfg.json.example

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"discord_link": "",
33
"token": "",
4+
"guild_id": "",
45
"discord_link_beta": "",
5-
"token_beta": ""
6+
"token_beta": "",
7+
"guild_id_beta": ""
68
}

Program.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ private class ConfigurationClass
2121
{
2222
public string discord_link = string.Empty;
2323
public string token = string.Empty;
24+
public ulong guild_id = 0;
2425
public string discord_link_beta = string.Empty;
2526
public string token_beta = string.Empty;
27+
public ulong guild_id_beta = 0;
2628

2729
public ConfigurationClass()
2830
{
2931
discord_link = string.Empty;
3032
token = string.Empty;
3133
token_beta = string.Empty;
3234
discord_link = string.Empty;
35+
guild_id = 0;
3336
}
3437
}
3538

@@ -71,7 +74,7 @@ public async Task MainAsync()
7174
await _client.StartAsync();
7275

7376
// Start Command Handler
74-
var commandHandler = new CommandHandler(_client, _commandService, _serviceProvider, BotConfiguration.Instance.Prefix);
77+
var commandHandler = new CommandHandler(_client, _commandService, _serviceProvider);
7578
await commandHandler.InstallCommandsAsync();
7679

7780
// Block this task until the program is closed.
@@ -106,6 +109,7 @@ private string InitializeBot()
106109
token = discordCfg.token_beta;
107110
BotConfiguration.Instance.DevMode = true;
108111
BotConfiguration.Instance.Discord_Link = discordCfg.discord_link_beta;
112+
BotConfiguration.Instance.Guild_Id = discordCfg.guild_id_beta;
109113
Console.WriteLine("==> Start in beta mode\n");
110114
}
111115
// Production Bot version
@@ -114,6 +118,7 @@ private string InitializeBot()
114118
token = discordCfg.token;
115119
BotConfiguration.Instance.DevMode = false;
116120
BotConfiguration.Instance.Discord_Link = discordCfg.discord_link;
121+
BotConfiguration.Instance.Guild_Id = discordCfg.guild_id;
117122
Console.WriteLine("==> Start in production mode\n");
118123
}
119124

@@ -185,7 +190,7 @@ private async Task InitializeCommands()
185190
// In dev mode, commands are only created on Guild
186191
if (IsDevMode() == true)
187192
{
188-
var guild = _client.GetGuild(#yourguild_id);
193+
var guild = _client.GetGuild(BotConfiguration.Instance.Guild_Id);
189194

190195
destination = guild;
191196
applicationCommands = await guild.GetApplicationCommandsAsync();

0 commit comments

Comments
 (0)