216 lines
7.2 KiB
Python
216 lines
7.2 KiB
Python
#!/usr/bin/env python3
|
|
import datetime
|
|
import json
|
|
from datetime import date
|
|
from pathlib import Path
|
|
import asyncio
|
|
|
|
import discord
|
|
from discord.ext import tasks
|
|
|
|
# Konstanten definieren------------------------------------------------------------------------------------
|
|
|
|
|
|
client = discord.Client(intents=discord.Intents.all())
|
|
|
|
glas_json = Path('/home/ne_ju/pythonProjects/discord/dasGlas/glas.json')
|
|
|
|
ch_coin = 'das-glas'
|
|
ch_witz = 'witzigkeit-kennt-keine-grenzen'
|
|
|
|
emo_coin = '\U0001FA99'
|
|
emo_sob = '\U0001f62d'
|
|
|
|
react_emo_money_with_wings = '\U0001F4B8'
|
|
react_emo_moneybag = '\U0001F4B0'
|
|
react_emo_smiling_face_with_tear = '\U0001f972'
|
|
|
|
snd_coin = '/home/ne_ju/pythonProjects/discord/dasGlas/coin.mp3'
|
|
snd_witz = '/home/ne_ju/pythonProjects/discord/dasGlas/kein_Pardon.mp3'
|
|
snd_test = '/home/ne_ju/pythonProjects/discord/dasGlas/test.mp3'
|
|
# JSON überprüfen/erstellen--------------------------------------------------------------------------------
|
|
|
|
|
|
if not glas_json.is_file():
|
|
glas_json.touch()
|
|
with open(glas_json, 'w') as f:
|
|
f.write('{}')
|
|
f.close()
|
|
|
|
|
|
# Sound abspielen------------------------------------------------------------------------------------------
|
|
|
|
async def play_sound(name, channel):
|
|
def dc(err):
|
|
async def wait_and_dc():
|
|
await asyncio.sleep(0.5)
|
|
await vc.disconnect()
|
|
fut = asyncio.run_coroutine_threadsafe(wait_and_dc(), client.loop)
|
|
try:
|
|
fut.result()
|
|
except:
|
|
print("error disconnecting")
|
|
|
|
if (channel == ch_coin):
|
|
sound = snd_coin
|
|
elif (channel == ch_witz):
|
|
sound = snd_witz
|
|
if not name.voice == None:
|
|
print(sound)
|
|
voicechannel = name.voice.channel
|
|
if voicechannel != None:
|
|
vc = await voicechannel.connect()
|
|
vc.play(discord.FFmpegPCMAudio(sound), after = dc)
|
|
|
|
|
|
# Stand erhöhen--------------------------------------------------------------------------------------------
|
|
|
|
|
|
def stand_plus(name, plus, channelname):
|
|
if channelname == ch_coin:
|
|
chanel = "coin"
|
|
elif channelname == ch_witz:
|
|
chanel = "witz"
|
|
with open(glas_json, "r+") as stand_json:
|
|
data = json.load(stand_json)
|
|
if not name in data:
|
|
data[name] = {"name": name, chanel: 0}
|
|
elif chanel not in data[name]:
|
|
data[name][chanel] = 0
|
|
data[name][chanel] = data[name][chanel] + plus
|
|
stand_json.seek(0)
|
|
stand_json.truncate()
|
|
json.dump(data, stand_json, indent=4, sort_keys=True)
|
|
|
|
|
|
# Aktuellen Stand ausgeben--------------------------------------------------------------------------------
|
|
|
|
|
|
async def stand_out(channel):
|
|
if channel.name == ch_coin:
|
|
chanel = "coin"
|
|
stdmsg = ['eine Münze in das Glas geworfen',
|
|
' Münzen in das Glas geworfen']
|
|
elif channel.name == ch_witz:
|
|
chanel = "witz"
|
|
stdmsg = ['einen schlechten Witz gemacht',
|
|
' schlechte Witze gemacht']
|
|
stand = list()
|
|
with open(glas_json, "r") as stand_json:
|
|
data = json.load(stand_json)
|
|
for x in data:
|
|
if chanel in data[x]:
|
|
stand.append([data[x][chanel], data[x]["name"]])
|
|
stand_json.close()
|
|
await channel.send('Der Stand ist:')
|
|
stand.sort(reverse=True)
|
|
for item in stand:
|
|
if item[0] == 1:
|
|
await channel.send(item[1] + ' hat ' + stdmsg[0])
|
|
else:
|
|
await channel.send(item[1] + ' hat ' + str(item[0]) + stdmsg[1])
|
|
|
|
|
|
# Reaktion auf Nachrichten--------------------------------------------------------------------------------
|
|
|
|
|
|
# \U0001FA99 - coin
|
|
# \U0001f62d - sob
|
|
# \U0001F4B8 - money with wings
|
|
# \U0001F4B0 - moneybag
|
|
# \U0001f972 - smiling face with tear
|
|
|
|
|
|
@client.event
|
|
async def on_ready():
|
|
print('We have logged in as {0.user}'.format(client))
|
|
output.start()
|
|
|
|
|
|
@client.event
|
|
async def on_message(message):
|
|
if message.author == client.user:
|
|
return
|
|
|
|
if message.channel.name == ch_coin:
|
|
emo = emo_coin
|
|
react_emo = react_emo_moneybag
|
|
multi_react_emo = react_emo_money_with_wings
|
|
elif message.channel.name == ch_witz:
|
|
emo = emo_sob
|
|
react_emo = react_emo_smiling_face_with_tear
|
|
multi_react_emo = react_emo_smiling_face_with_tear
|
|
|
|
if message.content.count(emo) > 0:
|
|
if len(message.mentions) > 0:
|
|
await message.add_reaction(multi_react_emo)
|
|
for j in message.mentions:
|
|
stand_plus(j.display_name, message.content.count(
|
|
emo), message.channel.name)
|
|
else:
|
|
await message.add_reaction(react_emo)
|
|
stand_plus(message.author.display_name,
|
|
message.content.count(emo), message.channel.name)
|
|
await play_sound(message.author, message.channel.name)
|
|
elif message.content.startswith('!test'):
|
|
await message.delete()
|
|
await play_sound(message.author, message.channel.name)
|
|
elif message.content.startswith('!stand'):
|
|
await message.delete()
|
|
for x in message.author.roles:
|
|
if str(x) == "Meh":
|
|
await stand_out(message.channel)
|
|
else:
|
|
await message.delete()
|
|
|
|
|
|
# Automatische Stand ausgabe------------------------------------------------------------------------------
|
|
|
|
|
|
@tasks.loop(hours=1)
|
|
async def output():
|
|
for guild in client.guilds:
|
|
if guild.name == 'Bärenkatapult':
|
|
if date.today().day == 1 and datetime.datetime.now().hour == 6:
|
|
await stand_out(discord.utils.get(guild.channels, name=ch_witz))
|
|
await stand_out(discord.utils.get(guild.channels, name=ch_coin))
|
|
|
|
|
|
# Channel verschiebung------------------------------------------------------------------------------------
|
|
|
|
|
|
@client.event
|
|
async def on_voice_state_update(member, before, after):
|
|
if after.channel is not None:
|
|
channel = after.channel
|
|
if channel.category.name == "Spiele":
|
|
if discord.utils.get(after.channel.guild.categories, name="belebt") == None:
|
|
await member.guild.create_category("belebt")
|
|
B = discord.utils.get(
|
|
after.channel.guild.categories, name="belebt")
|
|
await B.edit(position=3)
|
|
await channel.edit(category=B)
|
|
if before.channel is not None:
|
|
if before.channel.category.name == "belebt" and len(before.channel.members) == 0:
|
|
S = discord.utils.get(
|
|
before.channel.guild.categories, name="Spiele")
|
|
channel = before.channel
|
|
cat = channel.category
|
|
await channel.edit(category=S)
|
|
if len(cat.channels) == 0:
|
|
await cat.delete()
|
|
sorchan = ["0"]
|
|
for i in S.channels:
|
|
sorchan.append(i.name)
|
|
sorchan.remove("0")
|
|
sorchan.sort()
|
|
for i in range(len(sorchan)):
|
|
await discord.utils.get(before.channel.guild.channels, name=sorchan[i]).edit(position=i)
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
client.run('ODE1Mzc0MDc0NDU5OTc5ODM2.YDreSA.q494wu3zvinCJ6QiffrWvoUVibc')
|
|
# --------------------------------------------------------------------------------------------------------
|