-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
bot.py
196 lines (156 loc) Β· 6.54 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
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
import json
import logging
from pathlib import Path
import discord
from discord.ext import commands
from utils import MyContext, Ticket, JsonStore, SqliteStore
"""
Options for logging:
logging.NOTSET - No logging
logging.DEBUG - All info
logging.INFO - Slightly less
logging.WARN - Only warnings and worse
logging.ERROR - Only errors and worse
"""
logging.basicConfig(
format="%(levelname)s | %(asctime)s | %(module)s | %(message)s",
datefmt="%d/%m/%Y %I:%M:%S %p",
level=logging.INFO,
)
log = logging.getLogger(__name__)
class Bot(commands.Bot):
def __init__(self):
super().__init__(
command_prefix="-",
case_insensitive=True,
intents=discord.Intents.all(),
activity=discord.Game(name=".new for a ticket"),
)
self.cwd = str(Path(__file__).parents[0])
# The category to make tickets in
self.category_id = None
# The channel to send logs to
self.log_channel_id = None
# Where to setup the on_reaction -> create new ticket
self.new_ticket_channel_id = None
# The staff role to add to tickets
self.staff_role_id = None
# The data storage medium to use (MUST implement utils.db.base.Base)
# self.ticket_db = JsonStore(storage_path="/bot_config/")
self.ticket_db = SqliteStore(storage_path="/bot_config/")
if not self.category_id \
or not self.log_channel_id \
or not self.new_ticket_channel_id \
or not self.staff_role_id:
raise RuntimeError("Please set all of these variables above")
if not isinstance(self.category_id, int):
raise RuntimeError("Expected category_id to be an int")
if not isinstance(self.log_channel_id, int):
raise RuntimeError("Expected log_channel_id to be an int")
if not isinstance(self.new_ticket_channel_id, int):
raise RuntimeError("Expected new_ticket_channel_id to be an int")
if not isinstance(self.staff_role_id, int):
raise RuntimeError("Expected staff_role_id to be an int")
async def get_context(self, message, *, cls=MyContext):
return await super().get_context(message, cls=cls)
async def on_ready(self):
log.info(f"{self.user.display_name} is up and ready to go!")
async def on_raw_reaction_add(self, payload):
log.debug("Parsing a raw reaction add")
if not await Ticket.validate_reaction_event(self, payload, ["π", "β
"]):
return
reaction = str(payload.emoji)
if (
payload.message_id == await self.ticket_db.get_ticket_setup_message_id()
and reaction == "β
"
):
log.info("Attempting to create a ticket via reaction.")
await Ticket.reaction_create_ticket(self, payload)
elif reaction == "π":
# Simply add a tick to the message
channel = self.get_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
await message.add_reaction("β
")
elif reaction == "β
":
# Time to delete the ticket!
log.info("Attempting to close a ticket via reaction.")
await Ticket.reaction_close_ticket(self, payload)
async def on_raw_reaction_remove(self, payload):
log.debug("Parsing a raw reaction remove")
if not await Ticket.validate_reaction_event(self, payload, ["π"]):
return
reaction = str(payload.emoji)
if reaction == "π":
# Simply remove a tick from the message
guild = bot.get_guild(payload.guild_id)
member = await guild.fetch_member(bot.user.id)
channel = bot.get_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
await message.remove_reaction("β
", member)
if __name__ == "__main__":
bot = Bot()
@bot.command(name="new", description="Create a new ticket.", usage="[subject]")
@commands.guild_only()
async def new(ctx, *, subject=None):
log.info("Trying to create a new ticket via command.")
await ctx.ticket.create_ticket(subject=subject)
@bot.command(
name="sudonew",
description="Create a ticket on behalf of a user",
usage="<user>",
)
@commands.guild_only()
@commands.is_owner()
async def sudonew(ctx, user: discord.Member):
log.info("Trying to create a new ticket on behalf via command.")
await ctx.ticket.create_ticket(subject="Sudo Ticket Creation", sudo_author=user)
@bot.command(name="setup", description="Initial setup of the bot.")
@commands.guild_only()
@commands.is_owner()
async def setup(ctx):
log.info("Setting up the ticket bot.")
await ctx.ticket.setup_new_ticket_message()
@bot.command(
name="echo",
description="Repeat some text back using the bot.",
usage="<channel> <message>",
)
@commands.guild_only()
@commands.is_owner()
async def echo(ctx, channel: discord.TextChannel, *, content):
log.info(f"Echoing '{content}' as the bot")
await ctx.message.delete()
embed = discord.Embed(
description=content, color=0x808080, timestamp=ctx.message.created_at
)
embed.set_author(
name=ctx.guild.me.display_name, icon_url=ctx.guild.me.avatar_url
)
await channel.send(embed=embed)
@bot.command(
name="removeuser",
description="Removes a user from this ticket.",
usage="<user>",
)
@commands.guild_only()
@commands.has_role(bot.staff_role_id)
async def removeuser(ctx, user: discord.Member):
log.info(f"Attempting to remove {user.display_name} from a ticket.")
await ctx.ticket.remove_user(user)
@bot.command(
name="adduser", description="Add a user to this ticket", usage="<user>"
)
@commands.guild_only()
@commands.has_role(bot.staff_role_id)
async def adduser(ctx, user: discord.Member):
log.info(f"Attempted to add {user.display_name} to a ticket")
await ctx.ticket.add_user(user)
@bot.command(name="close", description="Close this ticket.", usage="[reason]")
@commands.guild_only()
async def close(ctx, *, reason=None):
log.info("Attempting to close a ticket via command.")
await ctx.ticket.close_ticket(reason=reason)
# <-- Start the bot -->
with open(bot.cwd + "/bot_config/token.json", "r") as file:
secret_file = json.load(file)
bot.run(secret_file["token"])