Skip to content

Instantly share code, notes, and snippets.

@kadet1090
Last active April 5, 2017 22:30
Show Gist options
  • Save kadet1090/8ffbe73c834c2e91531d8a848fd38afd to your computer and use it in GitHub Desktop.
Save kadet1090/8ffbe73c834c2e91531d8a848fd38afd to your computer and use it in GitHub Desktop.
Some discord bot for counting scores

Dd

Installation

python3 -m pip install -U discord.py

To be honest, I don't really remember if I had used anything beyond discord.py - if so, have fun figuring it out. I also highly encourage you to use virtualenvs.

Usage

All you need to do is to change the last line of the script and put there your's bot secret token:

client.run('psst, its a secret')

and then you can start it with

python bot.py

glhf

not working?

So go ahead and fix it.

license

Do what the fuck you want

import discord
import asyncio
import re
client = discord.Client()
regex = {
'score': re.compile('<@!?(.*?)>\s*\+\s*(\d+|\+)')
}
def get_score(score):
return max(0, min(1 if score == '+' else int(score), 50))
def get_nick(member):
return member.nick or member.name
def save_scores(scores, path):
file = open(path, "w")
file.write('\n'.join(['{} {}'.format(id, score) for id, score in scores.items() ]))
file.close()
def load_scores(path):
try:
scores = {}
file = open(path, "r")
for line in file:
(id, score) = line.split(' ')
scores[id] = int(score)
return scores
except FileNotFoundError:
return {}
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
@client.event
async def on_message(message):
matches = regex['score'].finditer(message.content)
for match in matches:
user, score = match.group(1), get_score(match.group(2))
if message.author.id != user:
try:
scores[user] += score
except KeyError:
scores[user] = score
print("User: {} += {} == {}".format(user, score, scores[user]))
save_scores(scores, "scores.txt")
matches = re.search('<@!?{}>\s+(\S+)(?:\s*(.*?))?'.format(message.server.me.id), message.content)
if matches:
try:
command, args = matches.group(1), matches.group(2)
print("C: {} A: {}".format(command, args))
if command in ['stats', 'staty', 'stat']:
await client.send_message(message.channel, '\n'.join(['{}: {}'.format(get_nick(message.server.get_member(id)), score) for id, score in scores.items()]))
except AttributeError:
pass
if __name__ == '__main__':
scores = load_scores("scores.txt")
client.run('psst, its a secret')
@naruciakk
Copy link

+10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment