Skip to content

Commit d41aa63

Browse files
committed
✨ added sync and unsync commands, code reformat
1 parent 0aeb9de commit d41aa63

File tree

8 files changed

+105
-65
lines changed

8 files changed

+105
-65
lines changed

bot.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313
import random
1414
import sqlite3
1515
import sys
16-
1716
from contextlib import closing
1817

1918
import discord
2019
from discord import Interaction
21-
from discord.ext import tasks, commands
22-
from discord.ext.commands import Bot
23-
from discord.ext.commands import Context
20+
from discord.ext import commands, tasks
21+
from discord.ext.commands import Bot, Context
2422

2523
import exceptions
2624

@@ -71,9 +69,10 @@
7169
7270
If you want to use prefix commands, make sure to also enable the intent below in the Discord developer portal.
7371
"""
74-
intents.message_content = True
72+
# intents.message_content = True
7573

76-
bot = Bot(command_prefix=commands.when_mentioned_or(config["prefix"]), intents=intents, help_command=None)
74+
bot = Bot(command_prefix=commands.when_mentioned_or(
75+
config["prefix"]), intents=intents, help_command=None)
7776

7877

7978
def init_db():
@@ -97,6 +96,7 @@ def connect_db():
9796
bot.config = config
9897
bot.db = connect_db()
9998

99+
100100
@bot.event
101101
async def on_ready() -> None:
102102
"""
@@ -108,7 +108,6 @@ async def on_ready() -> None:
108108
print(f"Running on: {platform.system()} {platform.release()} ({os.name})")
109109
print("-------------------")
110110
status_task.start()
111-
await bot.tree.sync(guild=bot.get_guild(bot.config["dev_guild_id"])) # This will load only the 'sync' and 'unsync' commands for the dev guild
112111

113112

114113
@tasks.loop(minutes=1.0)
@@ -145,7 +144,8 @@ async def on_command_completion(context: Context) -> None:
145144
print(
146145
f"Executed {executed_command} command in {context.guild.name} (ID: {context.guild.id}) by {context.author} (ID: {context.author.id})")
147146
else:
148-
print(f"Executed {executed_command} command by {context.author} (ID: {context.author.id}) in DMs")
147+
print(
148+
f"Executed {executed_command} command by {context.author} (ID: {context.author.id}) in DMs")
149149

150150

151151
@bot.event
@@ -222,4 +222,4 @@ async def load_cogs() -> None:
222222

223223
init_db()
224224
asyncio.run(load_cogs())
225-
bot.run(config["token"])
225+
bot.run(config["token"])

cogs/fun.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from discord import app_commands
1414
from discord.ext import commands
1515
from discord.ext.commands import Context
16-
1716
from helpers import checks
1817

1918

@@ -66,7 +65,8 @@ async def callback(self, interaction: discord.Interaction):
6665
bot_choice_index = choices[bot_choice]
6766

6867
result_embed = discord.Embed(color=0x9C84EF)
69-
result_embed.set_author(name=interaction.user.name, icon_url=interaction.user.avatar.url)
68+
result_embed.set_author(
69+
name=interaction.user.name, icon_url=interaction.user.avatar.url)
7070

7171
if user_choice_index == bot_choice_index:
7272
result_embed.description = f"**That's a draw!**\nYou've chosen {user_choice} and I've chosen {bot_choice}."
@@ -104,7 +104,7 @@ def __init__(self, bot):
104104
async def randomfact(self, context: Context) -> None:
105105
"""
106106
Get a random fact.
107-
107+
108108
:param context: The hybrid command context.
109109
"""
110110
# This will prevent your bot from stopping everything when doing a web request - see: https://discordpy.readthedocs.io/en/stable/faq.html#how-do-i-make-a-web-request
@@ -132,7 +132,7 @@ async def randomfact(self, context: Context) -> None:
132132
async def coinflip(self, context: Context) -> None:
133133
"""
134134
Make a coin flip, but give your bet before.
135-
135+
136136
:param context: The hybrid command context.
137137
"""
138138
buttons = Choice()
@@ -164,7 +164,7 @@ async def coinflip(self, context: Context) -> None:
164164
async def rock_paper_scissors(self, context: Context) -> None:
165165
"""
166166
Play the rock paper scissors game against the bot.
167-
167+
168168
:param context: The hybrid command context.
169169
"""
170170
view = RockPaperScissorsView()

cogs/moderation.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
from discord import app_commands
1111
from discord.ext import commands
1212
from discord.ext.commands import Context
13-
14-
from helpers import checks
15-
from helpers import db_manager
13+
from helpers import checks, db_manager
1614

1715

1816
class Moderation(commands.Cog, name="moderation"):
@@ -112,7 +110,7 @@ async def nick(self, context: Context, user: discord.User, nickname: str = None)
112110
async def ban(self, context: Context, user: discord.User, reason: str = "Not specified") -> None:
113111
"""
114112
Bans a user from the server.
115-
113+
116114
:param context: The hybrid command context.
117115
:param user: The user that should be banned from the server.
118116
:param reason: The reason for the ban. Default is "Not specified".
@@ -176,7 +174,8 @@ async def warning_add(self, context: Context, user: discord.User, reason: str =
176174
:param reason: The reason for the warn. Default is "Not specified".
177175
"""
178176
member = context.guild.get_member(user.id) or await context.guild.fetch_member(user.id)
179-
total = db_manager.add_warn(user.id, context.guild.id, context.author.id, reason)
177+
total = db_manager.add_warn(
178+
user.id, context.guild.id, context.author.id, reason)
180179
embed = discord.Embed(
181180
title="User Warned!",
182181
description=f"**{member}** was warned by **{context.author}**!\nTotal warns for this user: {total}",
@@ -227,13 +226,13 @@ async def warning_add(self, context: Context, user: discord.User, warn_id: int)
227226
async def warning_list(self, context: Context, user: discord.User):
228227
"""
229228
Shows the warnings of a user in the server.
230-
229+
231230
:param context: The hybrid command context.
232231
:param user: The user you want to get the warnings of.
233232
"""
234233
warnings_list = db_manager.get_warnings(user.id, context.guild.id)
235234
embed = discord.Embed(
236-
title = f"Warnings of {user}",
235+
title=f"Warnings of {user}",
237236
color=0x9C84EF
238237
)
239238
description = ""

cogs/owner.py

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,60 +14,97 @@
1414
from discord import app_commands
1515
from discord.ext import commands
1616
from discord.ext.commands import Context
17-
18-
from helpers import checks
19-
from helpers import db_manager
17+
from helpers import checks, db_manager
2018

2119
if not os.path.isfile("config.json"):
2220
sys.exit("'config.json' not found! Please add it and try again.")
2321
else:
2422
with open("config.json") as file:
2523
config = json.load(file)
2624

25+
2726
class Owner(commands.Cog, name="owner"):
2827
def __init__(self, bot):
2928
self.bot = bot
30-
31-
DEV_GUILD = discord.Object(id=config["dev_guild_id"])
3229

33-
@commands.hybrid_command(
30+
@commands.command(
3431
name="sync",
3532
description="Synchonizes the slash commands.",
3633
)
34+
@app_commands.describe(scope="The scope of the sync. Can be `global` or `guild`")
3735
@checks.is_owner()
38-
@app_commands.guilds(DEV_GUILD)
39-
async def sync(self, context: Context) -> None:
36+
async def sync(self, context: Context, scope: str) -> None:
4037
"""
4138
Synchonizes the slash commands.
4239
43-
:param context: The hybrid command context.
40+
:param context: The command context.
41+
:param scope: The scope of the sync. Can be `global` or `guild`.
4442
"""
45-
await self.bot.tree.sync()
46-
embed = discord.Embed(
47-
title="Slash Commands Sync",
48-
description="Slash commands have been synchronized.",
49-
)
50-
await context.send(embed=embed)
5143

52-
@commands.hybrid_command(
44+
if scope == "global":
45+
await context.bot.tree.sync()
46+
embed = discord.Embed(
47+
title="Slash Commands Sync",
48+
description="Slash commands have been globally synchronized.",
49+
color=0x9C84EF
50+
)
51+
await context.send(embed=embed)
52+
elif scope == "guild":
53+
context.bot.tree.copy_global_to(guild=context.guild)
54+
await context.bot.tree.sync(guild=context.guild)
55+
embed = discord.Embed(
56+
title="Slash Commands Sync",
57+
description="Slash commands have been synchronized in this guild.",
58+
color=0x9C84EF
59+
)
60+
await context.send(embed=embed)
61+
else:
62+
embed = discord.Embed(
63+
title="Invalid Scope",
64+
description="The scope must be `global` or `guild`.",
65+
color=0xE02B2B
66+
)
67+
await context.send(embed=embed)
68+
69+
@commands.command(
5370
name="unsync",
5471
description="Unsynchonizes the slash commands.",
5572
)
73+
@app_commands.describe(scope="The scope of the sync. Can be `global`, `current_guild` or `guild`")
5674
@checks.is_owner()
57-
@app_commands.guilds(DEV_GUILD)
58-
async def unsync(self, context: Context) -> None:
75+
async def unsync(self, context: Context, scope: str) -> None:
5976
"""
6077
Unsynchonizes the slash commands.
6178
62-
:param context: The hybrid command context.
79+
:param context: The command context.
80+
:param scope: The scope of the sync. Can be `global`, `current_guild` or `guild`.
6381
"""
64-
self.bot.tree.clear_commands(guild=None)
65-
await self.bot.tree.sync()
66-
embed = discord.Embed(
67-
title="Slash Commands Unsync",
68-
description="Slash commands have been unsynchronized.",
69-
)
70-
await context.send(embed=embed)
82+
83+
if scope == "global":
84+
context.bot.tree.clear_commands(guild=None)
85+
await context.bot.tree.sync()
86+
embed = discord.Embed(
87+
title="Slash Commands Unsync",
88+
description="Slash commands have been globally unsynchronized.",
89+
color=0x9C84EF
90+
)
91+
await context.send(embed=embed)
92+
elif scope == "guild":
93+
context.bot.tree.clear_commands(guild=context.guild)
94+
await context.bot.tree.sync(guild=context.guild)
95+
embed = discord.Embed(
96+
title="Slash Commands Unsync",
97+
description="Slash commands have been unsynchronized in this guild.",
98+
color=0x9C84EF
99+
)
100+
await context.send(embed=embed)
101+
else:
102+
embed = discord.Embed(
103+
title="Invalid Scope",
104+
description="The scope must be `global` or `guild`.",
105+
color=0xE02B2B
106+
)
107+
await context.send(embed=embed)
71108

72109
@commands.hybrid_command(
73110
name="load",
@@ -151,7 +188,7 @@ async def reload(self, context: Context, cog: str) -> None:
151188
title="Reload",
152189
description=f"Successfully reloaded the `{cog}` cog."
153190
)
154-
await context.send(embed=embed)
191+
await context.send(embed=embed)
155192

156193
@commands.hybrid_command(
157194
name="shutdown",

cogs/template.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
from discord.ext import commands
1010
from discord.ext.commands import Context
11-
1211
from helpers import checks
1312

1413

@@ -17,8 +16,8 @@ class Template(commands.Cog, name="template"):
1716
def __init__(self, bot):
1817
self.bot = bot
1918

20-
2119
# Here you can just add your own commands, you'll always need to provide "self" as first parameter.
20+
2221
@commands.hybrid_command(
2322
name="testcommand",
2423
description="This is a testing command that does nothing.",

config.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"token": "YOUR_BOT_TOKEN_HERE",
44
"permissions": "YOUR_BOT_PERMISSIONS_HERE",
55
"application_id": "YOUR_APPLICATION_ID_HERE",
6-
"dev_guild_id": "YOUR_DEVELOPMENT_GUILD_ID_HERE",
76
"owners": [
87
123456789,
98
987654321

helpers/checks.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
"""
88

99
import json
10-
from typing import TypeVar, Callable
10+
from typing import Callable, TypeVar
1111

1212
from discord.ext import commands
13-
1413
from exceptions import *
1514

1615
from helpers import db_manager

0 commit comments

Comments
 (0)