-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
146 lines (118 loc) · 5.69 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os
import discord
from discord.ext import commands, tasks
from discord.utils import get
import utils.records as records
import utils.database as database
import utils.reactionHandler as react
from utils.messages import verifyMessage, pendingVerify, Verification
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = int(os.getenv('DISCORD_GUILD'))
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix = '!', intents=intents)
#print(TOKEN)
#client = discord.Client(intents=intents)
@bot.event
async def on_ready():
#print(bot.guilds)
#print(GUILD)
#guild = discord.utils.get(bot.guilds, id=GUILD)
#print(guild)
print(
f'{bot.user} has connected to the Discord guilds:\n'
f'{bot.guilds}'
)
for guild in bot.guilds:
pendingVerify[guild.id] = []
try:
database.databaseConnections[guild.id] = database.dbConnect(guild.id)
except database.sqlite3.OperationalError:
database.databaseConnections[guild.id] = database.initializeDatabase(guild.id)
#members = '\n - '.join([member.name for member in guild.members])
#print(members)
#print(f'Guild Members:\n - {members}')
# listen for reactions being added to bot messages and handle the event
@bot.event
async def on_raw_reaction_add(payload: discord.RawReactionActionEvent):
# Ignore bot generated reactions
if (payload.user_id != bot.user.id):
#pass
reactionGuild = discord.utils.get(bot.guilds, id=payload.guild_id)
reactionChannel = discord.utils.get(reactionGuild.text_channels, id=payload.channel_id)
reactionMessage = await reactionChannel.fetch_message(payload.message_id)
# Respond to reactions to bot messages
if reactionMessage.author.id == bot.user.id:
for item in pendingVerify[reactionGuild.id]:
if payload.user_id == item.commandRequest.author.id and reactionMessage == item.verifyMessage:
# Red X or Green Check emojis used by bot verification messages
if payload.emoji.name == '\u274c':
pass
elif payload.emoji.name == '\u2705':
# verification logic/function
#await reactionMessage.channel.send("Yay!")
await addGameVerified(reactionMessage.channel,item.game, item.suggestor.name)
break
# Define command to have bot fetch the user who is picking the next game and the date they are picking for.
@bot.command()
async def whosnext(ctx):
nextMemberData = records.loadNextChoice()
memberID = nextMemberData[0]
nextDate = nextMemberData[1]
guild = discord.utils.get(bot.guilds, id=GUILD)
nextMember = discord.utils.get(guild.members, id=memberID)
await ctx.send(f"The person choosing the next game is {nextMember.display_name}! We'll be playing their game on {nextDate}.")
@bot.command()
async def setTurn(ctx):
nextDate = records.nextGameNight()
textPhrases = ctx.message.content.split()
if len(textPhrases) == 1:
records.saveNextChoice(ctx.author.id, nextDate)
await ctx.send(f'Alright, you\'re picking the game next time {ctx.author.name}! The next game night is {nextDate}.')
elif len(textPhrases) == 2:
try:
user = ctx.message.mentions[0]
records.saveNextChoice(user.id, nextDate)
await ctx.send(f'Looks like {user.name}\'s picking next time! The next game night is {nextDate}.')
except:
await ctx.send("Invalid command input")
else:
await ctx.send("Invalid command input, please use format \"!setTurn <@usermention>\" OR just use \"!setTurn\" (For example: !setTurn @Terlen OR !setTurn to set yourself)")
@bot.command()
async def addGame(ctx):
commandText = ctx.message.content.split()
if len(commandText) > 3:
commandText = ctx.message.content.split("\"")
if len(commandText) == 3:
# Order bot to create a verification message
addRequest = Verification(ctx)
addRequest.game = commandText[1]
addRequest.suggestor = ctx.message.mentions[0]
await verifyMessage(addRequest,operation="add",game=addRequest.game)
else:
await ctx.send(f"Invalid command. Make sure you're quoting if there are spaces in the name! (For example: \"The Game of Life\")")
async def addGameVerified(channel, game, name):
dbConnection = database.databaseConnections[channel.guild.id]
dbCursor = dbConnection.cursor()
with dbConnection:
try:
database.addGame(dbCursor, game, name)
await channel.send(f"Alright, {game} was added to our play history! Hope it was a good choice {name}!")
except database.sqlite3.OperationalError:
await channel.send(f"There was an error adding the game, please try again later.")
# If the player isn't in the database yet, foreign key check will fail and raise integrity error
except database.sqlite3.IntegrityError:
await channel.send(f"That player isn't in the database yet, please add them first then add this game.")
@bot.command()
async def addPlayer(ctx):
commandText = ctx.message.content.split()
if len(commandText) == 2:
database.dbAddPlayerRecord(database.databaseConnections[ctx.message.guild.id], ctx.message.mentions[0].id, ctx.message.mentions[0].name)
await ctx.send(f"Alright, player {ctx.message.mentions[0].name} has been recorded!")
@bot.command()
async def mercy(ctx):
commandText = ctx.message.content.split()
await ctx.send(f"I am not a merciful god, but this indiscretion shall be allowed.")
bot.run(TOKEN)