forked from cosmixdust/cheesebot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
294 lines (241 loc) · 8.39 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import asyncio
import random
import typing
from datetime import datetime
import re
import aiosqlite
import pytz
import discord
from discord import app_commands
from discord.ext import commands, tasks
from facts import cheeseFacts
from pics import cheesePhoto
from words import wordsList
intents = discord.Intents(
message_content=True,
messages=True,
guilds=True,
)
bot = commands.Bot(command_prefix="*", intents=intents)
def make_help_embed(author: discord.User | discord.Member) -> discord.Embed:
return discord.Embed(
title="About cheeseBot...",
description="Here's everything you need to know!",
color=discord.Color.random(),
timestamp=datetime.now()
).add_field(
name="Who am I?",
value="I'm cheeseBot. A bot made by <@396376536070094848> related to cheese stuff.",
).add_field(
name="Commands?",
value="/cheese, /fact, /pic, /blacklist, /announcements",
).add_field(
name="What's your privacy policy?",
value="You can check our privacy policy [here](https://github.com/deltastro/cheesebot/blob/master/policy.md)"
).set_author(
name=author.name,
icon_url=author.avatar,
)
@tasks.loop(seconds=1)
async def timeCheck():
"""
Send "CHEESE SUNDAY." every Sunday at noon.
"""
# At given time
now = datetime.now(pytz.utc)
if not all((
now.weekday() == 6,
now.hour == 12,
now.minute == 00)):
return # Time doesn't match
# Query the database
async with aiosqlite.connect("main.db") as db:
async with db.execute("SELECT id FROM ids") as cursor:
# For every result
async for row in cursor:
# Send to channel
channel_id = row[0]
channel = bot.get_partial_messageable(channel_id)
try:
await channel.send("CHEESE SUNDAY.")
except Exception as e:
print(e)
await asyncio.sleep(60)
@bot.event
async def on_ready():
"""
Tell us when we've connected.
"""
# on ready stuff
print("GIVE ME THE CHEESE.")
await bot.change_presence(activity=discord.Game("cheese."))
# Connect to the database and create tables if they don't exist
async with aiosqlite.connect("main.db") as db:
await db.execute("CREATE TABLE IF NOT EXISTS ids (id INTEGER, guild INTEGER)")
await db.execute("CREATE TABLE IF NOT EXISTS blacklist (user_id INTEGER)")
await db.commit()
# Start the timeCheck task
timeCheck.start()
# Sync commands and print the number of commands synced
synced = await bot.tree.sync()
print(f"Synced {len(synced)} command(s)")
@bot.tree.command(name="help")
async def helpCommand(interaction: discord.Interaction):
"""
use this to know more
"""
await interaction.response.send_message(embed=make_help_embed(interaction.user))
@bot.tree.command(name="cheese")
async def cheeseCommand(interaction: discord.Interaction):
"""
cheese
"""
await interaction.response.send_message("\N{CHEESE WEDGE}")
@bot.tree.command(name="fact")
async def cheeseFactCommand(interaction: discord.Interaction):
"""
cheese fact
"""
await interaction.response.send_message(random.choice(cheeseFacts))
@bot.tree.command(name="pic")
async def cheesePics(interaction: discord.Interaction):
"""
cheese pic
"""
await interaction.response.send_message(random.choice(cheesePhoto))
@bot.tree.command(name="blacklist")
async def blackList(interaction: discord.Interaction):
"""
Blacklist yourself from the trigger DMs (using the command again will whitelist you.)
"""
async with aiosqlite.connect("main.db") as db:
cursor = await db.execute(
"SELECT user_id FROM blacklist WHERE user_id = ?",
(interaction.user.id,),
)
data = await cursor.fetchone()
if data:
await db.execute(
"DELETE FROM blacklist WHERE user_id = ?",
(interaction.user.id,),
)
message = "You have been removed from the blacklist. You will now receive DMs."
else:
await db.execute(
"INSERT INTO blacklist (user_id) VALUES (?)",
(interaction.user.id,),
)
message = "You have been added to the blacklist. You will no longer receive DMs."
await db.commit()
await interaction.response.send_message(message)
@bot.tree.command(name="announcements")
@app_commands.describe(channel="The channel for the announcements.")
@app_commands.checks.has_permissions(administrator=True)
async def manage_announcements(interaction: discord.Interaction, channel: typing.Optional[discord.TextChannel] = None):
"""
Set a channel for announcements (admins only).
"""
assert interaction.guild
async with aiosqlite.connect("main.db") as db:
cursor = await db.execute(
"SELECT id FROM ids WHERE guild = ?",
(interaction.guild.id,),
)
data = await cursor.fetchone()
if not channel:
# Unsubscribe from announcements
if data:
await cursor.execute(
"DELETE FROM ids WHERE guild = ?",
(interaction.guild.id,),
)
await interaction.response.send_message(
"Got it! You will no longer receive cheese announcements!"
)
else:
await interaction.response.send_message(
"You're not subscribed to cheese announcements!",
ephemeral=True,
)
else:
# Subscribe to announcements
if channel.guild != interaction.guild:
return await interaction.response.send_message("That's not a channel on this server!", ephemeral=True)
if data:
await db.execute(
"UPDATE ids SET id = ? WHERE guild = ?",
(channel.id, interaction.guild.id),
)
else:
await db.execute(
"INSERT INTO ids (id, guild) VALUES (?, ?)",
(channel.id, interaction.guild.id),
)
await interaction.response.send_message(
(
f"Congrats! Your channel set for cheese announcements "
f"is now {channel.mention}"
)
)
await db.commit()
# no_cheese = r"""[\s!"£$%^&*()\[\]{}'@#~;:,<.>\/?\-\\\|`¬]"""
no_cheese = r"""[\s!"'\-,.?]*"""
cheese_regex = re.compile(
"({0})".format("|".join([
"(?:" + "".join([p + no_cheese for p in re.escape(i)]) + ")"
for i in wordsList
])),
re.IGNORECASE | re.DOTALL
)
def check_content_for_cheese(content: str) -> str | None:
"""
Check a string for if it contains any cheese keywords. If it does, then the
message content with the flagged keywords in bold is returned, otherwise
None.
Parameters
----------
content : str
The content that you want to check.
Returns
-------
str | None
The flagged content, or ``None``.
"""
changed = cheese_regex.sub("**\\1**", content)
if changed == content:
return None
return changed
@bot.event
async def on_message(message: discord.Message):
"""
Look for a message being sent, see if it contains cheese.
"""
if message.author.bot:
return
cheese_content = check_content_for_cheese(message.content)
if cheese_content is None:
return
send_dm: bool = True
async with aiosqlite.connect("main.db") as db:
async with db.execute("SELECT user_id FROM blacklist") as cursor:
async for row in cursor:
if row[0] == message.author.id:
send_dm = False
await message.add_reaction("🧀")
if not send_dm:
return False
embed = discord.Embed(
title="Cheese Detected!",
# description=f"You said: **{cheese_content}**",
description=cheese_content,
color=discord.Color.random(),
timestamp=datetime.now()
).set_footer(
text="Use /blacklist to not receive DMs anymore",
)
await message.author.send(embeds=[embed])
# fileToken = open("token.txt", "r")
# token = fileToken.read()
import sys
token = sys.argv[-1]
bot.run(token)