1
0

refactoring

This commit is contained in:
Oreh 2023-07-25 19:04:53 +08:00
parent cb33113e8a
commit 16723fee4d
7 changed files with 182 additions and 206 deletions

View File

@ -36,7 +36,6 @@ namespace Discord_Bot
return;
var context = new SocketCommandContext(_client, message);
int argPos = 0;
JObject config = Functions.GetConfig();

View File

@ -0,0 +1,179 @@
using Discord.Commands;
using Discord.WebSocket;
using System;
using System.Threading.Tasks;
using Discord;
namespace Discord_Bot
{
public class ExampleModule : ModuleBase<SocketCommandContext>
{
// .say hello world -> hello world
[Command("say")]
[Summary("Echoes a message.")]
public async Task SayAsync([Remainder] [Summary("The text to echo")] string echo)
{
RemoveLastUserMessage();
await ReplyAsync(echo);
}
private async void RemoveLastUserMessage()
{
var channel = Context.Channel as SocketTextChannel;
var items = await channel.GetMessagesAsync(1).FlattenAsync();
await channel.DeleteMessagesAsync(items);
}
[Command("hello")] // Command name.
[Summary("Say hello to the bot.")] // Command summary.
public async Task Hello()
=> await ReplyAsync($"Hello there, **{Context.User.Username}**!");
[Command("pick")]
[Alias("choose")] // Aliases that will also trigger the command.
[Summary("Pick something.")]
public async Task Pick([Remainder]string message = "")
{
string[] options = message.Split(new string[] { " or " }, StringSplitOptions.RemoveEmptyEntries);
string selection = options[new Random().Next(options.Length)];
// ReplyAsync() is a shortcut for Context.Channel.SendMessageAsync()
await ReplyAsync($"I choose **{selection}**");
}
[Command("cookie")]
[Summary("Give someone a cookie.")]
public async Task Cookie(SocketGuildUser user)
{
if (Context.Message.Author.Id == user.Id)
await ReplyAsync($"{Context.User.Mention} doesn't have anyone to share a cookie with... :(");
else
await ReplyAsync($"{Context.User.Mention} shared a cookie with **{user.Username}** :cookie:");
}
[Command("amiadmin")]
[Summary("Check your administrator status")]
public async Task AmIAdmin()
{
if ((Context.User as SocketGuildUser).GuildPermissions.Administrator)
await ReplyAsync($"Yes, **{Context.User.Username}**, you're an admin!");
else
await ReplyAsync($"No, **{Context.User.Username}**, you're **not** an admin!");
}
[Command("kick")]
[Summary("Kick a user from the server.")]
[RequireBotPermission(GuildPermission.KickMembers)]
[RequireUserPermission(GuildPermission.KickMembers)]
public async Task Kick(SocketGuildUser targetUser, [Remainder]string reason = "No reason provided.")
{
await targetUser.KickAsync(reason);
await ReplyAsync($"**{targetUser}** has been kicked. Bye bye :wave:");
}
[Command("ban")]
[Summary("Ban a user from the server")]
[RequireUserPermission(GuildPermission.BanMembers)]
[RequireBotPermission(GuildPermission.BanMembers)]
public async Task Ban(SocketGuildUser targetUser, [Remainder]string reason = "No reason provided.")
{
await Context.Guild.AddBanAsync(targetUser.Id, 0, reason);
await ReplyAsync($"**{targetUser}** has been banned. Bye bye :wave:");
}
[Command("unban")]
[Summary("Unban a user from the server")]
[RequireBotPermission(GuildPermission.BanMembers)]
[RequireUserPermission(GuildPermission.BanMembers)]
public async Task Unban(ulong targetUser)
{
await Context.Guild.RemoveBanAsync(targetUser);
await Context.Channel.SendMessageAsync($"The user has been unbanned :clap:");
}
[Command("purge")]
[Summary("Bulk deletes messages in chat")]
[RequireBotPermission(GuildPermission.ManageMessages)]
[RequireUserPermission(GuildPermission.ManageMessages)]
public async Task Purge(int delNumber)
{
var channel = Context.Channel as SocketTextChannel;
var items = await channel.GetMessagesAsync(delNumber + 1).FlattenAsync();
await channel.DeleteMessagesAsync(items);
}
[Command("reloadconfig")]
[Summary("Reloads the config and applies changes")]
[RequireOwner] // Require the bot owner to execute the command successfully.
public async Task ReloadConfig()
{
await Functions.SetBotStatusAsync(Context.Client);
await ReplyAsync("Reloaded!");
}
[Command("ping")]
[Summary("Show current latency.")]
public async Task Ping()
=> await ReplyAsync($"Latency: {Context.Client.Latency} ms");
[Command("avatar")]
[Alias("getavatar")]
[Summary("Get a user's avatar.")]
public async Task GetAvatar([Remainder]SocketGuildUser user = null)
=> await ReplyAsync($":frame_photo: **{(user ?? Context.User as SocketGuildUser).Username}**'s avatar\n{Functions.GetAvatarUrl(user)}");
[Command("info")]
[Alias("server", "serverinfo")]
[Summary("Show server information.")]
[RequireBotPermission(GuildPermission.EmbedLinks)] // Require the bot the have the 'Embed Links' permissions to execute this command.
public async Task ServerEmbed()
{
double botPercentage = Math.Round(Context.Guild.Users.Count(x => x.IsBot) / Context.Guild.MemberCount * 100d, 2);
EmbedBuilder embed = new EmbedBuilder()
.WithColor(0, 225, 225)
.WithDescription(
$"🏷️\n**Guild name:** {Context.Guild.Name}\n" +
$"**Guild ID:** {Context.Guild.Id}\n" +
$"**Created at:** {Context.Guild.CreatedAt:dd/M/yyyy}\n" +
$"**Owner:** {Context.Guild.Owner}\n\n" +
$"💬\n" +
$"**Users:** {Context.Guild.MemberCount - Context.Guild.Users.Count(x => x.IsBot)}\n" +
$"**Bots:** {Context.Guild.Users.Count(x => x.IsBot)} [ {botPercentage}% ]\n" +
$"**Channels:** {Context.Guild.Channels.Count}\n" +
$"**Roles:** {Context.Guild.Roles.Count}\n" +
$"**Emotes: ** {Context.Guild.Emotes.Count}\n\n" +
$"🌎 **Region:** {Context.Guild.VoiceRegionId}\n\n" +
$"🔒 **Security level:** {Context.Guild.VerificationLevel}")
.WithImageUrl(Context.Guild.IconUrl);
await ReplyAsync($":information_source: Server info for **{Context.Guild.Name}**", embed: embed.Build());
}
[Command("role")]
[Alias("roleinfo")]
[Summary("Show information about a role.")]
public async Task RoleInfo([Remainder]SocketRole role)
{
// Just in case someone tries to be funny.
if (role.Id == Context.Guild.EveryoneRole.Id)
return;
await ReplyAsync(
$":flower_playing_cards: **{role.Name}** information```ini" +
$"\n[Members] {role.Members.Count()}" +
$"\n[Role ID] {role.Id}" +
$"\n[Hoisted status] {role.IsHoisted}" +
$"\n[Created at] {role.CreatedAt:dd/M/yyyy}" +
$"\n[Hierarchy position] {role.Position}" +
$"\n[Color Hex] {role.Color}```");
}
// Please don't remove this command. I will appreciate it a lot <3
[Command("source")]
[Alias("sourcecode", "src")]
[Summary("Link the source code used for this bot.")]
public async Task Source()
=> await ReplyAsync($":heart: **{Context.Client.CurrentUser}** is based on this source code:\nhttps://github.com/VACEfron/Discord-Bot-Csharp");
}
}

View File

@ -1,48 +0,0 @@
using Discord.Commands;
using Discord.WebSocket;
using System;
using System.Threading.Tasks;
namespace Discord_Bot
{
public class FunSample : ModuleBase<SocketCommandContext>
{
[Command("hello")] // Command name.
[Summary("Say hello to the bot.")] // Command summary.
public async Task Hello()
=> await ReplyAsync($"Hello there, **{Context.User.Username}**!");
[Command("pick")]
[Alias("choose")] // Aliases that will also trigger the command.
[Summary("Pick something.")]
public async Task Pick([Remainder]string message = "")
{
string[] options = message.Split(new string[] { " or " }, StringSplitOptions.RemoveEmptyEntries);
string selection = options[new Random().Next(options.Length)];
// ReplyAsync() is a shortcut for Context.Channel.SendMessageAsync()
await ReplyAsync($"I choose **{selection}**");
}
[Command("cookie")]
[Summary("Give someone a cookie.")]
public async Task Cookie(SocketGuildUser user)
{
if (Context.Message.Author.Id == user.Id)
await ReplyAsync($"{Context.User.Mention} doesn't have anyone to share a cookie with... :(");
else
await ReplyAsync($"{Context.User.Mention} shared a cookie with **{user.Username}** :cookie:");
}
[Command("amiadmin")]
[Summary("Check your administrator status")]
public async Task AmIAdmin()
{
if ((Context.User as SocketGuildUser).GuildPermissions.Administrator)
await ReplyAsync($"Yes, **{Context.User.Username}**, you're an admin!");
else
await ReplyAsync($"No, **{Context.User.Username}**, you're **not** an admin!");
}
}
}

View File

@ -1,14 +0,0 @@
using Discord.Commands;
namespace Budet_cho_bot.Modules;
public class InfoModule : ModuleBase<SocketCommandContext>
{
// ~say hello world -> hello world
[Command("say")]
[Summary("Echoes a message.")]
public Task SayAsync([Remainder] [Summary("The text to echo")] string echo)
=> ReplyAsync(echo);
// ReplyAsync is a method on ModuleBase
}

View File

@ -1,60 +0,0 @@
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
namespace Discord_Bot
{
public class ModSample : ModuleBase<SocketCommandContext>
{
[Command("kick")]
[Summary("Kick a user from the server.")]
[RequireBotPermission(GuildPermission.KickMembers)]
[RequireUserPermission(GuildPermission.KickMembers)]
public async Task Kick(SocketGuildUser targetUser, [Remainder]string reason = "No reason provided.")
{
await targetUser.KickAsync(reason);
await ReplyAsync($"**{targetUser}** has been kicked. Bye bye :wave:");
}
[Command("ban")]
[Summary("Ban a user from the server")]
[RequireUserPermission(GuildPermission.BanMembers)]
[RequireBotPermission(GuildPermission.BanMembers)]
public async Task Ban(SocketGuildUser targetUser, [Remainder]string reason = "No reason provided.")
{
await Context.Guild.AddBanAsync(targetUser.Id, 0, reason);
await ReplyAsync($"**{targetUser}** has been banned. Bye bye :wave:");
}
[Command("unban")]
[Summary("Unban a user from the server")]
[RequireBotPermission(GuildPermission.BanMembers)]
[RequireUserPermission(GuildPermission.BanMembers)]
public async Task Unban(ulong targetUser)
{
await Context.Guild.RemoveBanAsync(targetUser);
await Context.Channel.SendMessageAsync($"The user has been unbanned :clap:");
}
[Command("purge")]
[Summary("Bulk deletes messages in chat")]
[RequireBotPermission(GuildPermission.ManageMessages)]
[RequireUserPermission(GuildPermission.ManageMessages)]
public async Task Purge(int delNumber)
{
var channel = Context.Channel as SocketTextChannel;
var items = await channel.GetMessagesAsync(delNumber + 1).FlattenAsync();
await channel.DeleteMessagesAsync(items);
}
[Command("reloadconfig")]
[Summary("Reloads the config and applies changes")]
[RequireOwner] // Require the bot owner to execute the command successfully.
public async Task ReloadConfig()
{
await Functions.SetBotStatusAsync(Context.Client);
await ReplyAsync("Reloaded!");
}
}
}

View File

@ -1,79 +0,0 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using System.Globalization;
namespace Discord_Bot
{
public class UtilitySample : ModuleBase<SocketCommandContext>
{
[Command("ping")]
[Summary("Show current latency.")]
public async Task Ping()
=> await ReplyAsync($"Latency: {Context.Client.Latency} ms");
[Command("avatar")]
[Alias("getavatar")]
[Summary("Get a user's avatar.")]
public async Task GetAvatar([Remainder]SocketGuildUser user = null)
=> await ReplyAsync($":frame_photo: **{(user ?? Context.User as SocketGuildUser).Username}**'s avatar\n{Functions.GetAvatarUrl(user)}");
[Command("info")]
[Alias("server", "serverinfo")]
[Summary("Show server information.")]
[RequireBotPermission(GuildPermission.EmbedLinks)] // Require the bot the have the 'Embed Links' permissions to execute this command.
public async Task ServerEmbed()
{
double botPercentage = Math.Round(Context.Guild.Users.Count(x => x.IsBot) / Context.Guild.MemberCount * 100d, 2);
EmbedBuilder embed = new EmbedBuilder()
.WithColor(0, 225, 225)
.WithDescription(
$"🏷️\n**Guild name:** {Context.Guild.Name}\n" +
$"**Guild ID:** {Context.Guild.Id}\n" +
$"**Created at:** {Context.Guild.CreatedAt:dd/M/yyyy}\n" +
$"**Owner:** {Context.Guild.Owner}\n\n" +
$"💬\n" +
$"**Users:** {Context.Guild.MemberCount - Context.Guild.Users.Count(x => x.IsBot)}\n" +
$"**Bots:** {Context.Guild.Users.Count(x => x.IsBot)} [ {botPercentage}% ]\n" +
$"**Channels:** {Context.Guild.Channels.Count}\n" +
$"**Roles:** {Context.Guild.Roles.Count}\n" +
$"**Emotes: ** {Context.Guild.Emotes.Count}\n\n" +
$"🌎 **Region:** {Context.Guild.VoiceRegionId}\n\n" +
$"🔒 **Security level:** {Context.Guild.VerificationLevel}")
.WithImageUrl(Context.Guild.IconUrl);
await ReplyAsync($":information_source: Server info for **{Context.Guild.Name}**", embed: embed.Build());
}
[Command("role")]
[Alias("roleinfo")]
[Summary("Show information about a role.")]
public async Task RoleInfo([Remainder]SocketRole role)
{
// Just in case someone tries to be funny.
if (role.Id == Context.Guild.EveryoneRole.Id)
return;
await ReplyAsync(
$":flower_playing_cards: **{role.Name}** information```ini" +
$"\n[Members] {role.Members.Count()}" +
$"\n[Role ID] {role.Id}" +
$"\n[Hoisted status] {role.IsHoisted}" +
$"\n[Created at] {role.CreatedAt:dd/M/yyyy}" +
$"\n[Hierarchy position] {role.Position}" +
$"\n[Color Hex] {role.Color}```");
}
// Please don't remove this command. I will appreciate it a lot <3
[Command("source")]
[Alias("sourcecode", "src")]
[Summary("Link the source code used for this bot.")]
public async Task Source()
=> await ReplyAsync($":heart: **{Context.Client.CurrentUser}** is based on this source code:\nhttps://github.com/VACEfron/Discord-Bot-Csharp");
}
}

View File

@ -2,9 +2,8 @@
"version": "1.0",
"token": "MTA4NDA5MTA4OTE1NTI4MDkxNw.G77VAz.3O8uS-D4nwp1Ax8iZMBIx0Z9gsYNNOwPFfGRl8",
"prefixes": ["."],
"join_message": "Hello, I'm Discord Bot! :heart:",
"currently": "playing|listening|watching|streaming",
"join_message": "Дратуте! :heart:",
"currently": "playing",
"playing_status": "майныч",
"status": "online|dnd|idle|offline",
"reminderTime": "1:12"
"status": "online"
}