Repository navigation
Expand file tree
/
Copy pathevent.py
More file actions
171 lines (132 loc) · 5.72 KB
/
Copy pathevent.py
File metadata and controls
171 lines (132 loc) · 5.72 KB
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
# event.py
import random
import sys
import traceback
import discord
from util import now, iferror, colorize
import responder
import autoload
ERROR_CHANNEL_ID = 942892215305396224
last_edit = 0
def load(core):
responders = core.exports.get('responder/all')
priority_responders = core.exports.get('responder/priority')
responder_toggle = core.exports.get_or_default('responder/toggle', False)
whitelists = core.exports.get('whitelists')
lang = core.exports.get('lang')
webhooks = core.exports.get('webhook/hooks')
hooks = core.exports.get('hooks')
# ============================== #
# ========== ON READY ========== #
# ============================== #
@core.bot.event
async def on_ready(): # once bot is connected to Discord
print(f'{core.bot.user} connected to Discord on {now(date_first=True)}')
autoload.load_asyncs(core)
await core.bot.get_channel(942838937427271771).send('i am here')
await core.bot.get_channel(859629975932895252).send('i am here')
await core.bot.tree.sync()
# game = discord.Game(name='_help')
# await core.bot.change_presence(activity=game)
# ============================== #
# ========== ON ERROR ========== #
# ============================== #
@core.bot.event
async def on_error(event, *args, **kwargs): # for unhandled errors
info = sys.exc_info()
if len(info) >= 2:
if 'Cannot send an empty message' in str(info[1]):
return
print('----------on_error----------')
error_printout = f'&cTime: {now(date_first=True)}\n'
error_printout += f'Discord information:\n{event}\n'
for a in args:
error_printout += str(a)
for k in kwargs:
error_printout += str(k)
if isinstance(args[0], discord.Message):
await args[0].channel.send(embed=iferror(lang()('error.unknown')))
error_printout += '\nMessage content: ' + args[0].content
error_printout += '\nPython information:'
error_printout += '\ntype: ' + str(info[0])
error_printout += '\nvalue: ' + str(info[1])
error_printout += '\ntraceback:\n'
error_printout += ''.join(traceback.format_tb(info[2]))
if len(error_printout) > 2000:
fp = f'logs/{now()}.txt'
with open(fp, 'w') as file:
file.write(error_printout)
discord_file = discord.File(fp)
await core.bot.get_channel(ERROR_CHANNEL_ID).send('Long error, here\'s a txt', file=discord_file)
else:
await core.bot.get_channel(ERROR_CHANNEL_ID).send(error_printout)
print(colorize(error_printout))
print('--------end on_error--------')
# ============================== #
# ========= ON MESSAGE ========= #
# ============================== #
@core.bot.event
async def on_message(message):
if message.author.id == core.bot.user.id: # ignore all messages from this bot and other bots
return
if w_id := message.webhook_id:
while webhooks() is None:
webhooks.load()
if w_id in webhooks():
try:
await webhooks()[w_id](message)
except Exception as e:
print(colorize('&cError responding to webhook message:'))
print(colorize(f'&cid: {w_id}'))
print(colorize(f'&cmessage: &r{message.content}'))
print(colorize(f'&c{str(e)}'))
await message.channel.send(random.choice([
'What was that?',
'What?',
'Can you repeat that?',
'lol what',
'bro what are you saying',
'ummm ok sweatie',
'you are making no sense',
'i have no idea',
]))
return
while responders() is None:
print('responder looked like it wasn\'t loaded, so i guess i have to do it myself...')
responder.load(core)
use_all = message.guild is not None and message.guild.id in whitelists()['autoreply'] and responder_toggle()
for r in (responders() if use_all else priority_responders()):
if await r.respond(message):
print(f'Just responded to a message at {now()}')
if r.continue_after:
continue
return
# ============================== #
# ========== ON REACT ========== #
# ============================== #
@core.bot.event
async def on_raw_reaction_add(payload):
if payload.member.id == core.bot.user.id:
return
channel = core.bot.get_guild(payload.guild_id).get_channel(payload.channel_id)
if channel is None:
return
message = await channel.fetch_message(payload.message_id)
if hooks():
for hook_obj in hooks()['member_join']:
await hook_obj.func(message, payload.emoji)
@core.bot.event
async def on_member_join(member):
if hooks():
for hook_obj in hooks()['member_join']:
await hook_obj.func(member)
@core.bot.event
async def on_load_require_async(module):
await module.load_async(core)
@core.bot.event
async def on_await_this(coro):
await coro
def await_this(coro):
core.bot.dispatch('await_this', coro)
core.exports.put('await_this', await_this)
print('Loaded event.py')