Skip to content

Commit

Permalink
Bot added.
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoine committed Jul 10, 2021
1 parent 420d13e commit cef6e88
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from src.game import blit
from src.game import get_user_name

from src.score import ScoreBot


@error_handler_wrapper
@curses_wrapper
Expand Down Expand Up @@ -109,3 +111,8 @@ def main(stdscr):

if __name__ == "__main__":
main()
bot = ScoreBot("NeoSnake", flush=False)
with open(".token.txt", 'r') as token_file:
token = token_file.readline()
bot.set_score(42)
bot.run(token)
84 changes: 84 additions & 0 deletions src/score.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import asyncio
import time

import discord
import numpy as np


class ScoreBot(discord.Client):
def __init__(self, score_board, flush=False):
super(ScoreBot, self).__init__()
self.score_board = score_board
self.score = None
self.flush = flush

async def on_ready(self):
print('Logged in as')
print(self.user.name)
print(self.user.id)
print('------')

print(self.is_ready())

await self.treat_score()

await self.close()

async def treat_score(self):
channel = self.get_channel(862644543207637042)
hist = await channel.history(limit=None).flatten()

for i, msg in enumerate(hist):
print(f"{msg.content}")
if self.flush:
await msg.delete()

if self.flush:
hist = await channel.history(limit=None).flatten()

print(len(hist))
if len(hist) == 0:
print("creating", self.score_board)
score_board = f"**{self.score_board}**:\n" \
f"\t1. {self.score}"
await channel.send(score_board)

else:
for msg in hist:
if msg.content.startswith(f"**{self.score_board}**"):
print("appending to", self.score_board)
score_board = msg.content.split('\n')
await msg.delete()
score_board += [f"\t{len(score_board)}. {self.score}"]
score_board, scores = score_board[0], score_board[1:]
scores = [int(score.strip().split(". ")[1]) for score in scores]
print(scores)
scores.sort()
scores = scores[::-1]
print(scores)
scores = ['\t' + ". ".join([str(rank + 1), str(score)]) for rank, score in
zip(range(len(scores)), scores)]
score_board = [score_board] + scores[:10]

await channel.send('\n'.join(score_board))

def set_score(self, score):
self.score = score


if __name__ == '__main__':
bot = ScoreBot("NeoSnake", flush=False)
with open(".token.txt", 'r') as token_file:
token = token_file.readline()
for score in np.random.randint(low=1, high=100, size=(20,)):
print(f"{score = }")
bot.set_score(score)
# bot.run(token)
# loop = asyncio.get_event_loop()
# asyncio.set_event_loop(asyncio.new_event_loop())
loop = asyncio.get_event_loop()
loop.run_until_complete(bot.start(token))
loop.close()
time.sleep(1)
# asyncio.set_event_loop(asyncio.new_event_loop())
bot.close()

0 comments on commit cef6e88

Please sign in to comment.