-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
56 lines (43 loc) · 1.32 KB
/
Copy pathbot.py
File metadata and controls
56 lines (43 loc) · 1.32 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
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
import sqlite3
load_dotenv()
TOKEN = os.getenv('DISCORD_API_TOKEN')
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
def setup_database():
conn = sqlite3.connect('bot_database.db')
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS user_stats (
user_id INTEGER PRIMARY KEY,
hello_count INTEGER DEFAULT 0
)
''')
conn.commit()
conn.close()
@bot.event
async def on_ready():
setup_database()
print(f'Logged in as {bot.user} (ID: {bot.user.id})')
print('------')
@bot.command()
async def hello(ctx):
user_id = ctx.author.id
conn = sqlite3.connect('bot_database.db')
c = conn.cursor()
c.execute('SELECT hello_count FROM user_stats WHERE user_id = ?', (user_id,))
result = c.fetchone()
if result is None:
c.execute('INSERT INTO user_stats (user_id, hello_count) VALUES (?, ?)', (user_id, 1))
count = 1
else:
count = result[0] + 1
c.execute('UPDATE user_stats SET hello_count = ? WHERE user_id = ?', (count, user_id))
conn.commit()
conn.close()
await ctx.send(f'Hello World! You have said hello to me {count} times.')
bot.run(TOKEN)