1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00
royalnet/discordbot.py

42 lines
1.3 KiB
Python
Raw Normal View History

2017-10-27 11:38:32 +00:00
import discord
from discord.ext import commands
import db
import re
import errors
# Init the event loop
import asyncio
loop = asyncio.get_event_loop()
# Init the config reader
import configparser
config = configparser.ConfigParser()
config.read("config.ini")
2017-10-30 09:46:37 +00:00
# Open a new postgres session
session = db.Session()
2017-10-27 11:38:32 +00:00
# Init the discord bot
client = discord.Client()
@client.event
async def on_message(message: discord.Message):
if message.content.startswith("!register"):
try:
username = message.content.split(" ", 1)[1]
except IndexError:
await client.send_message(message.channel, "⚠️ Non hai specificato un username!")
return
try:
2017-10-30 09:46:37 +00:00
d = db.Discord.create(session,
royal_username=username,
2017-10-27 11:38:32 +00:00
discord_user=message.author)
except errors.AlreadyExistingError:
await client.send_message(message.channel, "⚠ Il tuo account Discord è già collegato a un account RYG o l'account RYG che hai specificato è già collegato a un account Discord.")
return
2017-10-30 09:46:37 +00:00
session.add(d)
session.commit()
2017-10-27 11:38:32 +00:00
await client.send_message(message.channel, "✅ Sincronizzazione completata!")
client.run(config["Discord"]["bot_token"])
2017-10-30 09:46:37 +00:00
session.close()