Repository navigation
Expand file tree
/
Copy pathVeramaBot.py
More file actions
290 lines (241 loc) · 9.08 KB
/
Copy pathVeramaBot.py
File metadata and controls
290 lines (241 loc) · 9.08 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
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
# VeramaBot.py
import discord
import time
import os
import traceback
import logging
import logging.handlers
from time import localtime, strftime
from discord.ext import commands
from discord.ext.commands import Bot
from discord.ext import tasks
from dotenv import load_dotenv
from datetime import datetime
from cogs.Professions import updateProfessionBoard, LEADERBOARD_MESSAGE
from cogs.QuestSystem import oneStepQuestUpdate, pull_online_character_info, treasure_broadcast
from cogs.Roleplaying import RoleplayingButton
from cogs.Utilities import is_character_online
from functions.common import is_docker, editStatus, place_markers, fillThrallCages, update_boons, get_bot_config, \
set_bot_config
from cogs.CharRegistration import RegistrationButton
load_dotenv('data/server.env')
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
BOT_CHANNEL = int(os.getenv('BOT_CHANNEL'))
STATUS_CHANNEL = int(os.getenv('STATUS_CHANNEL'))
STATUS_MESSAGE = int(os.getenv('STATUS_MESSAGE'))
OUTCASTBOT_CHANNEL = int(os.getenv('OUTCASTBOT_CHANNEL'))
PROFESSION_CHANNEL = int(os.getenv('PROFESSION_CHANNEL'))
PROFESSION_MESSAGE = int(os.getenv('PROFESSION_MESSAGE'))
LEADERBOARD_MESSAGE = int(os.getenv('LEADERBOARD_MESSAGE'))
SERVER_PORT = int(os.getenv('SERVER_PORT'))
ADMIN_LOG_CHANNEL = int(os.getenv('ADMIN_LOG_CHANNEL'))
LOBBY_CHANNEL = int(os.getenv('LOBBY_CHANNEL'))
LOG_FILE_NAME = os.getenv('LOG_FILE_NAME')
intents = discord.Intents.all()
intents.message_content = True
if is_docker():
bot: Bot = commands.Bot(command_prefix=['v/', 'V/'], intents=intents)
else:
bot: Bot = commands.Bot(command_prefix=['vt/', 'Vt/'], intents=intents)
bot.quest_running = False
logger = logging.getLogger('conan')
logger.setLevel(logging.DEBUG)
handler = logging.handlers.RotatingFileHandler(
filename=f'{LOG_FILE_NAME}',
encoding='utf-8',
maxBytes=32 * 1024 * 1024, # 32 MiB
backupCount=5, # Rotate through 5 files
)
dt_fmt = '%Y-%m-%d %H:%M:%S'
formatter = logging.Formatter('[{asctime}] [{levelname:<8}] {name}: {message}', dt_fmt, style='{')
handler.setFormatter(formatter)
logger.addHandler(handler)
@bot.event
async def on_ready():
print(f'on ready run')
for f in os.listdir('./cogs'):
if f.endswith('.py'):
await bot.load_extension(f'cogs.{f[:-3]}')
loadtime = strftime('%m/%d/%y at %H:%M:%S', localtime(time.time()))
channel = bot.get_channel(BOT_CHANNEL)
bot.add_view(RegistrationButton())
bot.add_view(RoleplayingButton())
if is_docker():
# print(f'we are in docker')
if not liveStatus.is_running():
liveStatus.start()
if not onlineCharacterAlert.is_running():
onlineCharacterAlert.start()
if not professionBoard.is_running():
professionBoard.start()
if not onlineCharacterInfo.is_running():
onlineCharacterInfo.start()
if not oneStepQuestChecker.is_running():
oneStepQuestChecker.start()
if not placeMarkers.is_running():
placeMarkers.start()
if not boonChecker.is_running():
boonChecker.start()
if not fillCages.is_running():
fillCages.start()
else:
if not professionBoard.is_running():
professionBoard.start()
if not onlineCharacterInfo.is_running():
onlineCharacterInfo.start()
if not oneStepQuestChecker.is_running():
oneStepQuestChecker.start()
if not boonChecker.is_running():
boonChecker.start()
print(f'Bot in test mode, limited services are running.')
# if not treasure_announcer.is_running():
# treasure_announcer.start()
if is_docker():
await channel.send(f'VeramaBot PROD (use v/) started on {loadtime}.')
else:
await channel.send(f'VeramaBot TEST (use vt/) started on {loadtime}.')
print(f'on ready run finished')
@tasks.loop(seconds=30)
async def onlineCharacterInfo():
try:
pull_online_character_info()
except TimeoutError:
print(f'onlineCharacterInfo took too long to complete.')
return
except Exception:
print(f'onlineCharacterInfo ended with an exception')
return
@tasks.loop(seconds=45)
async def oneStepQuestChecker():
try:
await oneStepQuestUpdate(bot)
except TimeoutError:
print(f'oneStepQuestUpdate took too long to complete.')
return
except Exception as e:
print(f'oneStepQuestChecker ended with an exception {type(e)}')
traceback.format_exc()
return
@tasks.loop(minutes=8)
async def fillCages():
try:
fillThrallCages()
except TimeoutError:
print(f'fillThrallCages took too long to complete.')
return
except Exception as e:
print(f'fillThrallCages ended with an exception: {type(e)}')
return
@tasks.loop(minutes=5)
async def onlineCharacterAlert():
channel = bot.get_channel(BOT_CHANNEL)
# print(f'starting onlinecharalert')
try:
await is_character_online(channel)
except Exception as e:
print(f'onlineCharacterAlert ended with an exception {type(e)}')
return
@tasks.loop(minutes=1)
async def liveStatus():
channel = bot.get_channel(STATUS_CHANNEL)
try:
message = await channel.fetch_message(STATUS_MESSAGE)
# print(SERVER_PORT)
await editStatus(message, bot)
except discord.errors.DiscordServerError:
print(f'Discord error prevented status updates.')
return
except Exception as e:
print(f'liveStatus ended with an exception {type(e)}')
return
@tasks.loop(minutes=1)
async def professionBoard():
try:
channel = bot.get_channel(PROFESSION_CHANNEL)
message = await channel.fetch_message(PROFESSION_MESSAGE)
leaderboard_message = await channel.fetch_message(LEADERBOARD_MESSAGE)
except discord.errors.DiscordServerError:
print(f'Discord error prevented profession updates.')
return
try:
await updateProfessionBoard(message, leaderboard_message)
except discord.errors.DiscordServerError:
print(f'Discord error prevented profession updates.')
return
except Exception as e:
print(f'professionBoard ended with an exception {e}')
return
@tasks.loop(hours=1)
async def placeMarkers():
try:
place_markers()
#print(f'placing markers via loop')
except TimeoutError:
print(f'place_markers took too long to complete.')
return
except Exception:
print(f'place_markers ended with an exception')
return
@tasks.loop(minutes=10)
async def boonChecker():
try:
update_boons()
except TimeoutError:
print(f'boonChecker took too long to complete.')
return
except Exception:
print(f'boonChecker ended with an exception')
return
@tasks.loop(minutes=30)
async def treasure_announcer():
try:
treasure_broadcast()
except TimeoutError:
print(f'treasure_broadcast took too long to complete.')
@bot.event
async def on_member_join(member):
channel = bot.get_channel(ADMIN_LOG_CHANNEL)
guild = member.guild
if channel is not None:
await channel.send(f"{member.name} - {member.mention} has joined the server!")
channel2 = bot.get_channel(LOBBY_CHANNEL)
if channel2 is not None:
await channel2.send(f"Hello {member.name} - {member.mention}! Welcome to **Band of Outcasts**! <@&1024017048935874581>"
f"Please follow the server onboarding process and accept the rules here <id:customize>")
@bot.event
async def on_member_remove(member):
channel = bot.get_channel(ADMIN_LOG_CHANNEL)
guild = member.guild
if channel is not None:
await channel.send(f"{member.name} - {member.mention} has left the server! Join date: {member.joined_at}")
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send('Missing parameter! See v/help for details.')
return
if isinstance(error, commands.errors.CheckFailure):
print(f'Command from {ctx.message.author} failed checks. '
f'{ctx.message.channel.id}.')
channel = bot.get_channel(OUTCASTBOT_CHANNEL)
await ctx.send(f'You do not have permission to use this command, or you cannot use that command in this '
f'channel. Try {channel.mention}! SHAME {ctx.author.mention}!')
return
if isinstance(error, commands.errors.CommandOnCooldown):
await ctx.send(error)
return
if isinstance(error, commands.errors.BadArgument):
await ctx.send(error)
return
if isinstance(error, commands.errors.CommandNotFound):
await ctx.send(f'Invalid command `{ctx.message.content}`! Use `v/help`')
return
if isinstance(error, discord.errors.DiscordServerError):
channel = bot.get_channel(BOT_CHANNEL)
await channel.send(f'A discord server error has occurred. VeramaBot may need to be restarted to recover.')
return
else:
print(error)
await ctx.send(error)
raise error
bot.run(TOKEN, log_handler=None)