Basic functionalities

This commit is contained in:
Juan 2023-05-22 18:45:01 -04:00
parent 680d29c7d2
commit ae759b9153
3 changed files with 61 additions and 0 deletions

41
bot.py Normal file
View file

@ -0,0 +1,41 @@
import discord
import responses
async def send_message(message, user_message, is_private):
try:
response = responses.get_response(user_message)
await message.author.send(response) if is_private else await message.channel.send(response)
except Exception as e:
print(e)
def run_discord_bot():
TOKEN = 'MTAzMTgyODc5OTcyMzE1OTU1Mw.GuDYw4.NgEcEiljxvIUcDlr3WmD0X7TV5TByT_ob769QM'
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f'{client.user} is now running!')
@client.event
async def on_message(message):
if message.author == client.user:
return
username = str(message.author)
user_message = str(message.content)
channel = str(message.channel)
print(f'{username} said: "{user_message}" ({channel})')
if user_message[0] == '?':
user_message = user_message[1:]
await send_message(message, user_message, is_private=True)
else:
await send_message(message, user_message, is_private=False)
client.run(TOKEN)

4
main.py Normal file
View file

@ -0,0 +1,4 @@
import bot
if __name__ == '__main__':
bot.run_discord_bot()

16
responses.py Normal file
View file

@ -0,0 +1,16 @@
import random
def get_response(message: str) -> str:
p_message = message.lower()
if p_message == 'hello':
return 'Hey there!'
if message == 'roll':
return str(random.randint(1, 6))
if p_message == '!help':
return '`This is a help message that you can modify.`'
return 'I didn\'t understand what you wrote. Try typing "!help".'