-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
128 lines (110 loc) Β· 6.3 KB
/
bot.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
import asyncio # for debugging
import config
import discord
import os
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
filter_words = ["shit", "fuck"]
# console log when bot is ready
@client.event
async def on_ready():
print(f"Bot logged in as {client.user}")
@client.event
async def on_message(msg):
if msg.author != client.user: # to check that message wasn't from bot
# ping the bot
if msg.content.lower().startswith("!ping"):
await msg.channel.send(f"Hi, I am awake {msg.author.display_name}")
# word filter
for text in filter_words:
if "Moderator" not in str(msg.author.roles) and text in str(msg.content.lower()):
await msg.delete()
print("Deleted filtered word...")
return
# create note
if msg.content.lower().startswith("!createnote"):
if os.path.exists("note.txt") == False:
f = open("note.txt","a")
f.write(msg.content.split(" ", 1)[1]) # remove the command in my message
f.close()
await msg.channel.send(f"note.txt has been created and note has been saved {msg.author.display_name}")
else:
await msg.channel.send(f"there is already an existing note {msg.author.display_name}")
# read note
if msg.content.lower().startswith("!readnote"):
if os.path.exists("note.txt") == True:
f = open("note.txt", "r")
await msg.channel.send(f"The note says :{f.read()}")
else:
await msg.channel.send("There is no note to read!")
# delete note
if msg.content.lower().startswith("!deletenote"):
if os.path.exists("note.txt") == True:
os.remove("note.txt")
await msg.channel.send(f"deleted note.txt {msg.author.display_name}")
else:
await msg.channel.send("file does not exist, unable to delete note!")
# bot online status react message (displays the message)
if msg.content.lower().startswith("!botstatus"):
embed_bot_status = discord.Embed(title="Bot Status", description="React to the message to switch bot online status", type="rich", color=0x00ff00)
embed_bot_status.add_field(name="How to use", value="π’ = Online\nπ‘ = Idle\nπ΄ = Do not disturb\nβ = Close this prompt", inline=False)
sent_message = await msg.channel.send(embed=embed_bot_status)
await sent_message.add_reaction("π’")
await sent_message.add_reaction("π‘")
await sent_message.add_reaction("π΄")
await sent_message.add_reaction("β")
bot_message_id = sent_message.id # grab current message id from bot
# React to user reactions
@client.event
async def on_raw_reaction_add(payload):
message_id = payload.message_id
if message_id == bot_message_id:
if payload.emoji.name == "π’":
await client.change_presence(status=discord.Status.online) # change bot status to online
await sent_message.remove_reaction("π’", payload.member) # remove user reaction to make it look nice
await sent_message.delete(delay=60) # deletes message if user does not respond after awhile
await msg.delete(delay=60)
elif payload.emoji.name == "π‘":
await client.change_presence(status=discord.Status.idle)
await sent_message.remove_reaction("π‘", payload.member)
await sent_message.delete(delay=60)
await msg.delete(delay=60)
elif payload.emoji.name == "π΄":
await client.change_presence(status=discord.Status.dnd)
await sent_message.remove_reaction("π΄", payload.member)
await sent_message.delete(delay=60)
await msg.delete(delay=60)
elif payload.emoji.name == "β":
await sent_message.delete()
await msg.delete()
@client.event
async def on_raw_reaction_remove(payload):
pass
# purge/deletes all chat in text channel
if msg.content.lower().startswith("!purge"):
async with msg.channel.typing():
purged_messages = await msg.channel.purge(limit=50)
await msg.channel.send(f"Deleted {len(purged_messages)} message(s)")
# bot typing feature
if msg.content.lower().startswith("!typing"):
async with msg.channel.typing():
await asyncio.sleep(20)
await msg.channel.send("Done!")
# List all commands available currently for the bot
if msg.content.lower().startswith("!help"):
embed_command_list = discord.Embed(title="Command List", description="Displays all the current commands coded into the bot", color=0x00ff00)
file_image = discord.File("image.jpg", filename="image.jpg")
embed_command_list.set_image(url="attachment://image.jpg")
embed_command_list.add_field(name="!help", value="Displays this message", inline=False)
embed_command_list.add_field(name="!ping", value="Pings the bot", inline=False)
embed_command_list.add_field(name="!createnote", value="Creates a note with your content", inline=False)
embed_command_list.add_field(name="!readnote", value="Reads the current note if available", inline=False)
embed_command_list.add_field(name="!deletenote", value="Deletes the note", inline=False)
embed_command_list.add_field(name="!botstatus", value="Change bot online status", inline=False)
embed_command_list.add_field(name="!purge", value="Deletes 50 messages in text channel", inline=False)
embed_command_list.add_field(name="!typing", value="Debugging command", inline=False)
await msg.channel.send(embed=embed_command_list, file=file_image)
# optimize note taking feature using mongodb?
# add music function
client.run(config.token)