abhicodes commited on
Commit
b8f05dc
·
1 Parent(s): 61a7fd1

Create bot.py

Browse files
Files changed (1) hide show
  1. bot.py +50 -0
bot.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import discord
2
+ import mysql.connector
3
+ import os
4
+
5
+ mysql = mysql.connector.connect(
6
+ host=os.environ.get('DB_HOST'),
7
+ user=os.environ.get('DB_USER'),
8
+ password=os.environ.get('DB_PASSWORD'),
9
+ database=os.environ.get('DB_DATABASE'),
10
+ port=3306
11
+ )
12
+
13
+
14
+ async def send_message(message, server_name):
15
+ try:
16
+ await message.channel.send(f'Hello World {server_name}')
17
+ except Exception as e:
18
+ print(e)
19
+
20
+
21
+ def run_discord_bot():
22
+ cursor = mysql.cursor(dictionary=True)
23
+ cursor.execute("SELECT `token` FROM `auth_tokens` WHERE bot_id=%s", ((os.environ.get('BOT_ID')),))
24
+ bot_token = cursor.fetchone()
25
+ cursor.close()
26
+
27
+ TOKEN = bot_token['token']
28
+ intents = discord.Intents.default()
29
+ intents.message_content = True
30
+ client = discord.Client(intents=intents)
31
+
32
+ @client.event
33
+ async def on_ready():
34
+ print(f'{client.user} is now running!')
35
+
36
+ @client.event
37
+ async def on_message(message):
38
+ if message.author == client.user:
39
+ return
40
+
41
+ server_name = message.author.guild.name
42
+
43
+ if client.user.mentioned_in(message):
44
+ await send_message(message, server_name)
45
+
46
+ client.run(TOKEN)
47
+
48
+
49
+ if __name__ == '__main__':
50
+ run_discord_bot()