2023-06-19 19:10:03 -04:00
import time
2023-06-19 16:39:15 -04:00
import asyncio
import os
import aiohttp
import revolt
from revolt . ext import commands
2023-06-19 21:50:15 -04:00
import dotenv
2023-06-19 19:10:03 -04:00
from dotenv import load_dotenv
2023-06-19 16:39:15 -04:00
2023-06-19 21:50:15 -04:00
env = dotenv . find_dotenv ( )
load_dotenv ( env )
2023-06-19 16:39:15 -04:00
token = os . getenv ( ' TOKEN ' )
api_url = os . getenv ( ' API_URL ' )
2023-06-19 16:47:48 -04:00
prefix = os . getenv ( ' PREFIX ' )
2023-06-19 16:39:15 -04:00
2023-06-19 22:28:30 -04:00
class Client ( commands . CommandsClient ) :
2023-06-19 17:15:19 -04:00
# This class contains all of the commands the bot uses.
2023-06-20 01:05:42 -04:00
async def get_prefix ( self , message : revolt . Message , input : str | None = None ) : # pylint: disable=W0622
2023-06-20 01:02:22 -04:00
if input is None :
return prefix
2023-06-19 21:50:15 -04:00
return input
2023-06-20 01:06:40 -04:00
2023-06-20 01:02:22 -04:00
async def on_message ( self , message : revolt . Message ) :
2023-06-20 01:05:42 -04:00
if ' PREFIX ' not in os . environ or prefix is None :
2023-06-20 01:02:22 -04:00
if message . author == self . user . owner :
await message . channel . send ( " You have started the bot without setting the `prefix` environment variable! \n It has been set to `temp!` automatically, please change it using `temp!prefix <new prefix>`. " )
print ( " ERROR: prefix_env_var check failed! Prefix set to ' temp! ' . " )
new_prefix = " temp! "
await Client . prefix_change ( self = self , message = message , new_prefix = new_prefix , silent = True )
else :
print ( " ERROR: prefix_env_var check failed! " )
else :
if isinstance ( message . author , revolt . Member ) :
print ( f " { message . author . name } # { message . author . discriminator } ( { message . author . id } ): { message . content } \n ⤷ Sent from { message . server . name } ( { message . server . id } ) " )
else :
print ( f " { message . author . name } # { message . author . discriminator } ( { message . author . id } ): { message . content } \n ⤷ Sent in Direct Messages " )
await Client . process_commands ( self , message )
2023-06-19 17:15:19 -04:00
2023-06-19 16:39:15 -04:00
@commands.command ( )
async def ping ( self , ctx : commands . Context ) :
2023-06-19 19:01:43 -04:00
# This command checks the bot's latency.
before = time . monotonic ( )
2023-06-19 22:20:17 -04:00
await ctx . message . reply ( " 🏓 " )
2023-06-19 19:01:43 -04:00
mrm_list = await ctx . channel . history ( limit = 1 )
mrm = mrm_list [ 0 ]
ping = ( time . monotonic ( ) - before ) * 1000
embeds = [ revolt . SendableEmbed ( title = " 🏓 Pong! " , description = f " ` \n { int ( ping ) } ms` " , colour = " #5d82d1 " ) ]
2023-06-19 21:50:15 -04:00
await mrm . edit ( content = " " , embeds = embeds )
2023-06-19 19:01:43 -04:00
print ( f ' Ping { int ( ping ) } ms ' )
2023-06-19 16:39:15 -04:00
@commands.command ( )
2023-06-19 21:50:15 -04:00
async def avatar ( self , ctx : commands . Context , target : revolt . User ) :
# This command retrieves a user's avatar. CURRENTLY BROKEN
if not isinstance ( target , revolt . User ) :
2023-06-19 22:23:40 -04:00
await ctx . message . reply ( " Please provide a user argument! " )
2023-06-19 16:39:15 -04:00
return
2023-06-19 21:50:15 -04:00
avatar = target . avatar . url
2023-06-19 22:23:40 -04:00
await ctx . message . reply ( f " { avatar } " )
2023-06-19 16:39:15 -04:00
2023-06-20 01:02:22 -04:00
async def prefix_change ( self , message : revolt . Message , new_prefix : str , silent : bool | None = False ) :
2023-06-20 01:05:42 -04:00
dotenv . set_key ( env , ' PREFIX ' , new_prefix )
if silent is not True :
await message . reply ( f " Prefix has been changed from ` { prefix } ` to ` { new_prefix } `! " )
print ( f " Prefix changed: { prefix } → { new_prefix } " )
await Client . get_prefix ( message , new_prefix )
2023-06-20 01:02:22 -04:00
2023-06-19 21:50:15 -04:00
@commands.command ( )
async def prefix ( self , ctx : commands . Context , new_prefix : str = None ) :
2023-06-20 01:13:32 -04:00
# This command sets the bot's prefix. CURRENTLY BROKEN
if new_prefix is not None and ctx . author . id == Client . user . owner_id :
2023-06-20 01:02:22 -04:00
await Client . prefix_change ( self = self , message = ctx . message , new_prefix = new_prefix )
2023-06-19 21:50:15 -04:00
else :
2023-06-19 22:23:40 -04:00
await ctx . message . reply ( f " The prefix is currently set to ` { prefix } `. " )
2023-06-19 21:50:15 -04:00
2023-06-19 16:39:15 -04:00
async def main ( ) :
2023-06-19 17:11:20 -04:00
# This function logs into the bot user.
2023-06-19 16:39:15 -04:00
async with aiohttp . ClientSession ( ) as session :
2023-06-19 22:28:30 -04:00
client = Client ( session , token , api_url = api_url )
2023-06-19 16:39:15 -04:00
await client . start ( )
2023-06-19 17:11:20 -04:00
asyncio . run ( main ( ) )