discordbot/main.py
2019-04-16 13:40:11 +02:00

279 lines
12 KiB
Python

import discord
import asyncio
import random
import json
import os.path
import time
adminid = "190506444972359680"
client = discord.Client()
msgid = None
usermsg = None
players = {}
@client.event
async def on_ready():
print("Eingelogt als")
print(client.user.name)
print(client.user.id)
print("----------------")
await client.change_presence(game=discord.Game(name="auf Purple-Horizon-Clan.net"))
@client.event
async def on_message(message):
if message.content.lower().startswith("!info"):
await client.send_message(message.channel, "Der Bot wurde von Saenger.ItsWar#8555 erstellt!")
if message.content.lower().startswith("!coin"):
choice = random.randint(1, 2)
if choice == 1:
await client.send_message(message.channel, ":skull:")
if choice == 2:
await client.send_message(message.channel, ":eight:")
if message.content.lower().startswith("!getchover") and message.author.id == adminid:
perms = discord.channel.Overwrites
await client.send_message(message.channel, "Die Overwrites sind : perms".format(perms=perms))
if message.content.startswith("!game") and message.author.id == adminid:
game = message.content[6:]
await client.change_presence(game=discord.Game(name=game))
if message.content.lower().startswith("!help"):
try:
await client.send_message(message.channel, "__**Hier sind die Befehle die du Nutzen kannst:**__\n"
"```"
"!user @<user> | Um die informationen eines users zu sehen.\n"
" | <user> steht hier für den Usernamen der\n"
" | auf diesem server ist.\n"
" | bsp: !user @Saenger.ItsWar\n\n"
"!coin | für einen Münzwurf\n\n"
"!play <derytlink>| Um musik auf dem discord abzuspielen.\n"
" | Bsp: !play https://www.youtube.com/watch"
" | ?v=ZV9Dsj7ORak\n\n"
"!pause | Um die wiedergabe zu Pausieren.\n\n"
"!resume | Um die Wiedergabe fortzusetzen.\n\n"
"!join | Der Bot joined in deinen channel.\n\n"
"!quit | Der Bot Leaved deinen channel.\n\n"
"!role | Um dir selber rollen zu "
" | geben wie Steam,Uplay,....\n\n"
"!peanuts | Um deinen aktuellen Peanut(XP) stand zu"
" | sehen.```")
except Exception as error1:
await client.send_message(message.channel, "Ein Error: ```{error}```".format(error=error1))
if message.content.lower().startswith("!user"):
try:
user = message.mentions[0]
userjoindat = str(user.joined_at).split(".", 1)[0]
usercreated = str(user.created_at).split(".", 1)[0]
userembed = discord.Embed(
title="Username:",
description=user.name,
color=0x9b59b6
)
userembed.set_author(
name="User Info:"
)
userembed.add_field(
name="Spiel:",
value=user.game
)
userembed.add_field(
name="Status:",
value=user.status
)
userembed.add_field(
name="Beigetreten am:",
value=userjoindat
)
userembed.add_field(
name="Erstellt am:",
value=usercreated
)
userembed.add_field(
name="Discriminator:",
value=user.discriminator
)
userembed.add_field(
name="User ID:",
value=user.id
)
await client.send_message(message.channel, embed=userembed)
except IndexError:
await client.send_message(message.channel, "Ich konnte den gesuchten User nicht finden!")
except:
await client.send_message(message.channel, "Sorry Fehler!")
finally:
pass
if message.content.startswith('!join'):
try:
channel = message.author.voice.voice_channel
await client.join_voice_channel(channel)
except discord.errors.InvalidArgument:
await client.send_message(message.channel, "Kein Voice channel gefunden.")
except Exception as error:
await client.send_message(message.channel, "Ein Error: ```{error}```".format(error=error))
if message.content.startswith('!quit'):
try:
voice_client = client.voice_client_in(message.server)
await voice_client.disconnect()
except AttributeError:
await client.send_message(message.channel, "Ich bin zur zeit nicht connected.")
except Exception as error:
await client.send_message(message.channel, "Ein Error: ```{error}```".format(error=error))
if message.content.startswith('!play'):
try:
yt_url = message.content[6:]
if client.is_voice_connected(message.server):
try:
voice = client.voice_client_in(message.server)
players[message.server.id].stop()
player = await voice.create_ytdl_player(yt_url, before_options=' -reconnect 1 -reconnect_streamed 1'
' -reconnect_delay_max 5'
' --default-search "ytsearch"')
players[message.server.id] = player
player.start()
except Exception as e:
await client.send_message(message.server, "Error: [{error}]".format(error=e))
if not client.is_voice_connected(message.server):
try:
channel = message.author.voice.voice_channel
voice = await client.join_voice_channel(channel)
player = await voice.create_ytdl_player(yt_url, before_options=' -reconnect 1 -reconnect_streamed 1'
' -reconnect_delay_max 5'
' --default-search "ytsearch"')
players[message.server.id] = player
player.start()
except Exception as e:
await client.send_message(message.channel, "Error: [{error}]".format(error=e))
except Exception as e:
await client.send_message(message.channel, "Error: [{error}]".format(error=e))
if message.content.startswith('!pause'):
try:
players[message.server.id].pause()
except Exception as e:
await client.send_message(message.channel, "Error: [{error}]".format(error=e))
if message.content.startswith('!resume'):
try:
players[message.server.id].resume()
except Exception as e:
await client.send_message(message.channel, "Error: [{error}]".format(error=e))
if message.content.lower().startswith('!role'):
botmsg = await client.send_message(message.channel, "Wähle deine Servergruppe in dem du auf die jeweiligen "
"icons unter diser nachricht klickst")
global steam
steam = discord.utils.get(client.get_all_emojis(), name="steam")
global origin
origin = discord.utils.get(client.get_all_emojis(), name="origin")
global uplay
uplay = discord.utils.get(client.get_all_emojis(), name="uplay")
global bnet
bnet = discord.utils.get(client.get_all_emojis(), name="bnet")
global osu
osu = discord.utils.get(client.get_all_emojis(), name="osu")
await client.add_reaction(botmsg, steam)
await client.add_reaction(botmsg, origin)
await client.add_reaction(botmsg, uplay)
await client.add_reaction(botmsg, bnet)
await client.add_reaction(botmsg, osu)
global msgid
msgid = botmsg.id
global usermsg
usermsg = message.author
if message.content.lower().startswith('!peanuts'):
await client.send_message(message.channel, "Du hast `{}` PEANUTS!"
"<:peanut:451096369667768331>".format(get_xp(message.author.id)))
user_add_xp(message.author.id, 2)
# define the user getting xp added method
def user_add_xp(user_id: int, peanuts: int):
if os.path.isfile("users.json"):
try:
with open('users.json', 'r') as fp:
users = json.load(fp)
users[user_id]['peanuts'] += peanuts
with open('users.json', 'w') as fp:
json.dump(users, fp, sort_keys=True, indent=4)
except KeyError:
with open('users.json', 'r') as fp:
users = json.load(fp)
users[user_id] = {}
users[user_id]['peanuts'] = peanuts
with open('users.json', 'w') as fp:
json.dump(users, fp, sort_keys=True, indent=4)
else:
users = {user_id: {}}
users[user_id]['peanuts'] = peanuts
with open('users.json', 'w') as fp:
json.dump(users, fp, sort_keys=True, indent=4)
# define the xp reading method
def get_xp(user_id: int):
if os.path.isfile('users.json'):
with open('users.json', 'r') as fp:
users = json.load(fp)
return users[user_id]['peanuts']
else:
return 0
# Timed role assingning
# @client.event
# async def on_member_join(member):
# time.sleep(10)
# role = discord.utils.find(lambda r: r.name == "Steam", member.server.roles)
# await client.add_roles(member, role)
# Role assinging on reacting
@client.event
async def on_reaction_add(reaction, user):
msg = reaction.message
chat = reaction.message.channel
if reaction.emoji == steam and msg.id == msgid:
role = discord.utils.find(lambda r: r.name == "Steam", msg.server.roles)
await client.add_roles(user, role)
await client.send_message(chat, "✅|Erfolg! {0} Du hast nun die Servergruppe Steam".format(user.mention))
if reaction.emoji == origin and msg.id == msgid:
role = discord.utils.find(lambda r: r.name == "Origin", msg.server.roles)
await client.add_roles(user, role)
await client.send_message(chat, "✅|Erfolg! {0} Du hast nun die Servergruppe Origin".format(user.mention))
if reaction.emoji == uplay and msg.id == msgid:
role = discord.utils.find(lambda r: r.name == "Uplay", msg.server.roles)
await client.add_roles(user, role)
await client.send_message(chat, "✅|Erfolg! {0} Du hast nun die Servergruppe Uplay".format(user.mention))
if reaction.emoji == bnet and msg.id == msgid and user == usermsg:
role = discord.utils.find(lambda r: r.name == "Battle.net", msg.server.roles)
await client.add_roles(user, role)
await client.send_message(chat, "✅|Erfolg! {0} Du hast nun die Servergruppe "
"Battle.net".format(user.mention))
if reaction.emoji == osu and msg.id == msgid and user == usermsg:
role = discord.utils.find(lambda r: r.name == "OSU!", msg.server.roles)
await client.add_roles(user, role)
await client.send_message(chat, "✅|Erfolg! {0} Du hast nun die Servergruppe OSU!".format(user.mention))
client.run("client-id-here")