-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgary.py
140 lines (118 loc) · 5.32 KB
/
gary.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
import discord
import sys
import traceback
from discord.ext import commands
import config
from files.setup_logging import GaryLogging
# First we load the environment classes which hold the token and id of the different Gary bots.
try:
if sys.argv[1] == "DEV":
env = config.DEV()
except:
env = config.PRODUCTION()
# Then we establish that we are using the discord.Bot lib.
intents = discord.Intents.all()
bot = discord.Bot(intents=intents)
bot.id = env.id
bot.token = env.token
bot.serpapi_key = env.serpapi_key
bot.openai_key = env.openai_key
bot.openai_orginization = env.openai_orginization
bot.gallery_channel_name = env.gallery_channel_name
bot.lavalink_server_password = env.lavalink_server_password
bot.logger = GaryLogging()
# Then we start to load in all of the selected cogs in the cogs folder.
# Make sure "help" is the last element in this list.
cogs_to_load = ["ping", "burn", "askgary", "quote", "mock", "wavelink_music", "bbtcg", "bbtcg_games", "meow", "gif", "help"]
if __name__ == '__main__':
for cog in cogs_to_load:
try:
bot.load_extension("cogs." + cog)
except Exception as e:
# If something goes wrong this should print that a cog failed to load.
print(f'Failed to load cog {cog}.', file=sys.stderr)
traceback.print_exc()
bot.logger.log.error(f"Failed to load cog {cog}!")
input(" ")
# When Gary is ready, this is fired.
@bot.event
async def on_ready():
channels = bot.get_all_channels()
for c in channels:
if c.name == "admin-log":
bot.admin_log = c
elif c.name == bot.gallery_channel_name:
bot.gallery_channel = c
# This constructs all the background info for the help command.
command_helps = {}
for command in bot.commands:
#print(command.default_member_permissions)
if command.default_member_permissions != None:
continue
if "SlashCommandGroup" in str(type(command)):
#command_helps = {**command_helps, command.name: {"description": command.description, "type": "SlashCommandGroup"}}
for c in command.subcommands:
command_helps = {**command_helps, c.name: {"description": c.description, "parent": command.name, "type": "SlashCommand"}}
elif "SlashCommand" in str(type(command)):
command_helps = {**command_helps, command.name: {"description": command.description, "type": "SlashCommand"}}
elif "UserCommand" in str(type(command)):
command_helps = {**command_helps, command.name: {"description": command.__dict__["__original_kwargs__"]["help"], "type": "UserCommand"}}
elif "MessageCommand" in str(type(command)):
command_helps = {**command_helps, command.name: {"description": command.__dict__["__original_kwargs__"]["help"], "type": "MessageCommand"}}
else:
bot.logger.log.warn(f"Failed to parse /help for {command.name}")
bot.command_helps = command_helps
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
"~ Thanks for using Gary! ~\n"
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
print(f"Logged in as {bot.user}.")
# This catches any application command (slash command) errors.
# This includes cooldowns.
@bot.event
async def on_application_command_error(ctx, event):
if "Command" in str(event) and "is not found" in str(event):
await ctx.respond("Meow?")
elif "is a required argument that is missing" in str(event):
await ctx.respond(f"You are missing arguments to that command!")
elif isinstance(event, commands.CommandOnCooldown):
seconds = int(str(event)[34:-4])
seconds_in_day = 60 * 60 * 24
seconds_in_hour = 60 * 60
seconds_in_minute = 60
days = seconds // seconds_in_day
hours = (seconds - (days * seconds_in_day)) // seconds_in_hour
minutes = (seconds - (days * seconds_in_day) - (hours * seconds_in_hour)) // seconds_in_minute
seconds_left = seconds - (days * seconds_in_day) - (hours * seconds_in_hour) - (minutes * seconds_in_minute)
if days == 0:
days_output = ""
elif days == 1:
days_output = f" {days} day"
else:
days_output = f" {days} days"
if hours == 0:
hours_output = ""
elif hours == 1:
hours_output = f" {hours} hour"
else:
hours_output = f" {hours} hours"
if minutes == 0:
minutes_output = ""
elif minutes == 1:
minutes_output = f" {minutes} minute"
else:
minutes_output = f" {minutes} minutes"
if seconds_left == 0:
seconds_output = " <1 second"
elif seconds_left == 1:
seconds_output = f" {seconds_left} second"
else:
seconds_output = f" {seconds_left} seconds"
await ctx.respond(f"You can't do that yet! Try again in{days_output}{hours_output}{minutes_output}{seconds_output}!", ephemeral=True)
elif "CheckFailure" in str(type(event)):
await ctx.respond(f"Please use this command in the correct channel!", ephemeral=True)
else:
print(type(event))
print(f"\nSomething caused this error:\n{event}")
bot.logger.log.error(f"Something caused this error: {event}")
traceback.print_exc()
bot.run(bot.token)