forked from staciax/valorant-discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
107 lines (81 loc) · 2.98 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
from __future__ import annotations
import asyncio
import os
import sys
import traceback
import aiohttp
import discord
from discord.ext import commands
from discord.ext.commands import ExtensionFailed, ExtensionNotFound, NoEntryPointError
from dotenv import load_dotenv
from utils import locale_v2
from utils.valorant.cache import get_cache
load_dotenv()
initial_extensions = ['cogs.admin', 'cogs.errors', 'cogs.notify', 'cogs.valorant']
# intents required
intents = discord.Intents.default()
intents.message_content = True
BOT_PREFIX = '-'
# todo claer white space
class ValorantBot(commands.Bot):
debug: bool
bot_app_info: discord.AppInfo
def __init__(self) -> None:
super().__init__(command_prefix=BOT_PREFIX, case_insensitive=True, intents=intents)
self.session: aiohttp.ClientSession = None
self.bot_version = '3.3.5'
self.tree.interaction_check = self.interaction_check
@staticmethod
async def interaction_check(interaction: discord.Interaction) -> bool:
locale_v2.set_interaction_locale(interaction.locale) # bot responses localized # wait for update
locale_v2.set_valorant_locale(interaction.locale) # valorant localized
return True
@property
def owner(self) -> discord.User:
return self.bot_app_info.owner
async def on_ready(self) -> None:
await self.tree.sync()
print(f"\nLogged in as: {self.user}\n\n BOT IS READY !")
print(f"Version: {self.bot_version}")
# bot presence
activity_type = discord.ActivityType.listening
await self.change_presence(activity=discord.Activity(type=activity_type, name="(╯•﹏•╰)"))
async def setup_hook(self) -> None:
if self.session is None:
self.session = aiohttp.ClientSession()
try:
self.owner_id = int(os.getenv('OWNER_ID'))
except ValueError:
self.bot_app_info = await self.application_info()
self.owner_id = self.bot_app_info.owner.id
self.setup_cache()
await self.load_cogs()
# await self.tree.sync()
async def load_cogs(self) -> None:
for ext in initial_extensions:
try:
await self.load_extension(ext)
except (
ExtensionNotFound,
NoEntryPointError,
ExtensionFailed,
):
print(f'Failed to load extension {ext}.', file=sys.stderr)
traceback.print_exc()
@staticmethod
def setup_cache() -> None:
try:
open('data/cache.json')
except FileNotFoundError:
get_cache()
async def close(self) -> None:
await self.session.close()
await super().close()
async def start(self, debug: bool = False) -> None:
self.debug = debug
return await super().start(os.getenv('TOKEN'), reconnect=True)
def run_bot() -> None:
bot = ValorantBot()
asyncio.run(bot.start())
if __name__ == '__main__':
run_bot()