|
3 | 3 | Description:
|
4 | 4 | This is a template to create your own discord bot in python.
|
5 | 5 |
|
6 |
| -Version: 2.4.3 |
| 6 | +Version: 2.5 |
7 | 7 | """
|
8 | 8 |
|
9 |
| -import discord, asyncio, os, platform, sys |
| 9 | +import os |
| 10 | +import platform |
| 11 | +import random |
| 12 | +import sys |
| 13 | + |
| 14 | +import discord |
| 15 | +import yaml |
| 16 | +from discord.ext import commands, tasks |
10 | 17 | from discord.ext.commands import Bot
|
11 |
| -from discord.ext import commands |
12 | 18 |
|
13 |
| -if not os.path.isfile("config.py"): |
14 |
| - sys.exit("'config.py' not found! Please add it and try again.") |
| 19 | +if not os.path.isfile("config.yaml"): |
| 20 | + sys.exit("'config.yaml' not found! Please add it and try again.") |
15 | 21 | else:
|
16 |
| - import config |
| 22 | + with open("config.yaml") as file: |
| 23 | + config = yaml.load(file, Loader=yaml.FullLoader) |
17 | 24 |
|
18 | 25 | """
|
19 | 26 | Setup bot intents (events restrictions)
|
|
40 | 47 | intents.voice_states = False
|
41 | 48 | intents.webhooks = False
|
42 | 49 |
|
43 |
| -Privileged Intents (Needs to be enabled on dev page): |
| 50 | +Privileged Intents (Needs to be enabled on dev page), please use them only if you need them: |
44 | 51 | intents.presences = True
|
45 | 52 | intents.members = True
|
46 | 53 | """
|
47 | 54 |
|
48 |
| -intents = discord.Intents.all() |
| 55 | +intents = discord.Intents.default() |
| 56 | + |
| 57 | +bot = Bot(command_prefix=config["bot_prefix"], intents=intents) |
49 | 58 |
|
50 |
| -bot = Bot(command_prefix=config.BOT_PREFIX, intents=intents) |
51 | 59 |
|
52 | 60 | # The code in this even is executed when the bot is ready
|
53 | 61 | @bot.event
|
54 | 62 | async def on_ready():
|
55 |
| - bot.loop.create_task(status_task()) |
56 |
| - print(f"Logged in as {bot.user.name}") |
57 |
| - print(f"Discord.py API version: {discord.__version__}") |
58 |
| - print(f"Python version: {platform.python_version()}") |
59 |
| - print(f"Running on: {platform.system()} {platform.release()} ({os.name})") |
60 |
| - print("-------------------") |
| 63 | + print(f"Logged in as {bot.user.name}") |
| 64 | + print(f"Discord.py API version: {discord.__version__}") |
| 65 | + print(f"Python version: {platform.python_version()}") |
| 66 | + print(f"Running on: {platform.system()} {platform.release()} ({os.name})") |
| 67 | + print("-------------------") |
| 68 | + status_task.start() |
| 69 | + |
61 | 70 |
|
62 | 71 | # Setup the game status task of the bot
|
| 72 | +@tasks.loop(minutes=1.0) |
63 | 73 | async def status_task():
|
64 |
| - while True: |
65 |
| - await bot.change_presence(activity=discord.Game("with you!")) |
66 |
| - await asyncio.sleep(60) |
67 |
| - await bot.change_presence(activity=discord.Game("with Krypton!")) |
68 |
| - await asyncio.sleep(60) |
69 |
| - await bot.change_presence(activity=discord.Game(f"{config.BOT_PREFIX} help")) |
70 |
| - await asyncio.sleep(60) |
71 |
| - await bot.change_presence(activity=discord.Game("with humans!")) |
72 |
| - await asyncio.sleep(60) |
| 74 | + statuses = ["with you!", "with Krypton!", f"{config['bot_prefix']}help", "with humans!"] |
| 75 | + await bot.change_presence(activity=discord.Game(random.choice(statuses))) |
| 76 | + |
73 | 77 |
|
74 | 78 | # Removes the default help command of discord.py to be able to create our custom help command.
|
75 | 79 | bot.remove_command("help")
|
76 | 80 |
|
77 | 81 | if __name__ == "__main__":
|
78 |
| - for file in os.listdir("./cogs"): |
79 |
| - if file.endswith(".py"): |
80 |
| - extension = file[:-3] |
81 |
| - try: |
82 |
| - bot.load_extension(f"cogs.{extension}") |
83 |
| - print(f"Loaded extension '{extension}'") |
84 |
| - except Exception as e: |
85 |
| - exception = f"{type(e).__name__}: {e}" |
86 |
| - print(f"Failed to load extension {extension}\n{exception}") |
| 82 | + for file in os.listdir("./cogs"): |
| 83 | + if file.endswith(".py"): |
| 84 | + extension = file[:-3] |
| 85 | + try: |
| 86 | + bot.load_extension(f"cogs.{extension}") |
| 87 | + print(f"Loaded extension '{extension}'") |
| 88 | + except Exception as e: |
| 89 | + exception = f"{type(e).__name__}: {e}" |
| 90 | + print(f"Failed to load extension {extension}\n{exception}") |
| 91 | + |
87 | 92 |
|
88 | 93 | # The code in this event is executed every time someone sends a message, with or without the prefix
|
89 | 94 | @bot.event
|
90 | 95 | async def on_message(message):
|
91 |
| - # Ignores if a command is being executed by a bot or by the bot itself |
92 |
| - if message.author == bot.user or message.author.bot: |
93 |
| - return |
94 |
| - # Ignores if a command is being executed by a blacklisted user |
95 |
| - if message.author.id in config.BLACKLIST: |
96 |
| - return |
97 |
| - await bot.process_commands(message) |
| 96 | + # Ignores if a command is being executed by a bot or by the bot itself |
| 97 | + if message.author == bot.user or message.author.bot: |
| 98 | + return |
| 99 | + # Ignores if a command is being executed by a blacklisted user |
| 100 | + |
| 101 | + if message.author.id in config["blacklist"]: |
| 102 | + return |
| 103 | + await bot.process_commands(message) |
| 104 | + |
98 | 105 |
|
99 | 106 | # The code in this event is executed every time a command has been *successfully* executed
|
100 | 107 | @bot.event
|
101 | 108 | async def on_command_completion(ctx):
|
102 |
| - fullCommandName = ctx.command.qualified_name |
103 |
| - split = fullCommandName.split(" ") |
104 |
| - executedCommand = str(split[0]) |
105 |
| - print(f"Executed {executedCommand} command in {ctx.guild.name} (ID: {ctx.message.guild.id}) by {ctx.message.author} (ID: {ctx.message.author.id})") |
| 109 | + fullCommandName = ctx.command.qualified_name |
| 110 | + split = fullCommandName.split(" ") |
| 111 | + executedCommand = str(split[0]) |
| 112 | + print( |
| 113 | + f"Executed {executedCommand} command in {ctx.guild.name} (ID: {ctx.message.guild.id}) by {ctx.message.author} (ID: {ctx.message.author.id})") |
| 114 | + |
106 | 115 |
|
107 | 116 | # The code in this event is executed every time a valid commands catches an error
|
108 | 117 | @bot.event
|
109 | 118 | async def on_command_error(context, error):
|
110 |
| - if isinstance(error, commands.CommandOnCooldown): |
111 |
| - embed = discord.Embed( |
112 |
| - title="Error!", |
113 |
| - description="This command is on a %.2fs cool down" % error.retry_after, |
114 |
| - color=config.error |
115 |
| - ) |
116 |
| - await context.send(embed=embed) |
117 |
| - raise error |
| 119 | + if isinstance(error, commands.CommandOnCooldown): |
| 120 | + embed = discord.Embed( |
| 121 | + title="Error!", |
| 122 | + description="This command is on a %.2fs cool down" % error.retry_after, |
| 123 | + color=config["error"] |
| 124 | + ) |
| 125 | + await context.send(embed=embed) |
| 126 | + elif isinstance(error, commands.MissingPermissions): |
| 127 | + embed = discord.Embed( |
| 128 | + title="Error!", |
| 129 | + description="You are missing the permission `" + ", ".join( |
| 130 | + error.missing_perms) + "` to execute this command!", |
| 131 | + color=config["error"] |
| 132 | + ) |
| 133 | + await context.send(embed=embed) |
| 134 | + raise error |
| 135 | + |
118 | 136 |
|
119 | 137 | # Run the bot with the token
|
120 |
| -bot.run(config.TOKEN) |
| 138 | +bot.run(config["token"]) |
0 commit comments