-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscordBot.py
95 lines (67 loc) · 2.47 KB
/
discordBot.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
import os
import discord
from time import time, gmtime, strftime
from dotenv import load_dotenv
from rps import RPS
load_dotenv()
TOKEN = os.getenv('TOKEN')
SERVER = os.getenv('SERVER')
client = discord.Client()
rpsDict = {}
rpsKeys = { 'r': 0, 'rock': 0,
'p': 1, 'paper': 1,
's': 2, 'scissors': 2}
rpsNames = {'0': 'rock',
'1': 'paper',
'2': 'scissors'}
rpsOutKeys = { '-1': 'Computer Wins!',
'1' : 'Player Wins!',
'0' : 'You drew!'}
@client.event
async def on_ready():
guild = discord.utils.get(client.guilds, name=SERVER)
print(
f'{client.user} is connected to the following guild:\n'
f'{guild.name} (id: {guild.id})'
)
@client.event
async def on_message(message):
# skip if by bot
if str(message.author) == 'MangoPi#8181': return
# log message
file = 'log.csv'
with open(file, 'a', encoding="utf-8") as f:
f.write(f"""{strftime('%d/%m/%Y %H:%M:%S', gmtime(time()))},"{str(message.channel)}","{message.author}",{repr(message.content).replace("'",'"')}\n""")
# react for fun
# if str(message.author) == 'Sekai#2422':
# await message.add_reaction('👀')
# if str(message.author) == 'Mango#6990':
# await message.add_reaction('❤️')
# if str(message.author) == 'Bread Accountant#4781':
# await message.add_reaction('🤮')
# rps game
msg = message.content
ID = message.author.id
if msg.startswith('-rps'):
try:
arg = msg.split("-rps ", 1)[1] # splits once on '-rps ' and takes whats after (the input)
except IndexError:
return
# create class instance/get reference to existing
if ID not in rpsDict:
rps = RPS(str(message.author), ID, folder_path='rps logs')
rpsDict[ID] = rps
else:
rps = rpsDict[ID]
if arg in ['score', 'stats']:
s, w, l, d = rps.get_score()
await message.channel.send(f"```Total Games Played: {w+l+d}, Winrate: {w/float(l+w):.2f}\nScore: {s}, Wins: {w}, Losses: {l}, Draws: {d}```")
return
# check for valid input
if arg not in rpsKeys:
await message.channel.send(f"Input '{arg}' not recognised. Please try again.")
return
# play
cin, _, pout = rps.play(rpsKeys[arg])
await message.channel.send(f'Computer played {rpsNames[str(cin)]}\n{rpsOutKeys[str(pout)]}')
client.run(TOKEN)