Merhaba c# dilinde bir discord botuna admin komutu eklemek istiyorum fakat yapamadım. Yapmak istediğim şey, belirli bir komudu kullanınca (örneğin !ban) gibi bir id kontrolü yapmasını istiyorum. yani listede olmayan komutlar kullanılırsa admin kontrolü yapılmicak ama listede olan bir komut kullanılırsa admin kontrolü yapılcak.Fakat bot kullandığım her komudu kontrol edip kullanma izniniz yok diyor. Yardım edicek varmı?
C#:
namespace Supercell.Laser.Server.Discord
{
using System;
using System.Linq;
using System.Threading.Tasks;
using NetCord;
using NetCord.Gateway;
using NetCord.Services;
using NetCord.Services.Commands;
using Supercell.Laser.Server.Settings;
public class CustomLogger
{
public void Log(LogSeverity severity, string message, Exception exception = null)
{
string formattedMessage = $"[DISCORD] {message}";
if (severity == LogSeverity.Info)
{
Console.WriteLine(formattedMessage);
}
else
{
Console.WriteLine($"[{severity}] {formattedMessage}");
}
}
}
public class DiscordBot
{
private readonly CustomLogger _logger = new CustomLogger();
private GatewayClient _client;
private readonly ulong[] _allowedUserIds = { 1141268241117872158, 807674732547014686 };
private readonly string[] _allowedCommands = { "status", "komut2", "komut3" }; // İzin verilen komutlar
public async Task StartAsync()
{
_client = new GatewayClient(
new BotToken(Configuration.Instance.BotToken),
new GatewayClientConfiguration()
{
Intents =
GatewayIntents.GuildMessages
| GatewayIntents.DirectMessages
| GatewayIntents.MessageContent,
}
);
CommandService<CommandContext> commandService = new();
commandService.AddModules(typeof(DiscordBot).Assembly);
_client.MessageCreate += async message =>
{
if (!message.Content.StartsWith('!') || message.Author.IsBot)
return;
var commandName = message.Content.Substring(1).Split(' ')[0]; // Komut adını al
if (!_allowedCommands.Contains(commandName))
{
await message.ReplyAsync("Bu komutu kullanma izniniz yok.");
return;
}
// Kullanıcı kimliğini kontrol et
if (!_allowedUserIds.Contains(message.Author.Id))
{
await message.ReplyAsync("Bu komutu kullanma izniniz yok.");
return;
}
var result = await commandService.ExecuteAsync(
prefixLength: 1,
new CommandContext(message, _client)
);
if (result is IFailResult failResult)
{
try
{
await message.ReplyAsync(failResult.Message);
}
catch { }
}
};
_client.Log += message =>
{
_logger.Log(message.Severity, message.Message, message.Exception);
return default;
};
await _client.StartAsync();
}
}
}
