DiscordBots/dasGlas/old/14.11.21_main.py

210 lines
8.0 KiB
Python
Raw Normal View History

2025-04-22 08:40:05 +00:00
#!/usr/bin/env python3
import configparser
import discord
import datetime
from discord.ext import tasks
from pathlib import Path
from datetime import date
client = discord.Client()
coin_config = configparser.ConfigParser()
witz_config = configparser.ConfigParser()
coin_ini = Path('/home/pi/discord/dasGlas/coin.ini')
if not coin_ini.is_file():
coin_ini.touch()
ch_coin = 'das-glas'
witz_ini = Path('/home/pi/discord/dasGlas/witz.ini')
if not witz_ini.is_file():
witz_ini.touch()
ch_witz = 'witzigkeit-kennt-keine-grenzen'
# --------------------------------------------------------------------------------------------------------
async def dc(vc):
while True:
if not vc.is_playing():
await vc.disconnect()
return
# --------------------------------------------------------------------------------------------------------
async def play_coin(name):
voicechannel = name.voice.channel
if voicechannel != None:
vc = await voicechannel.connect()
vc.play(discord.FFmpegPCMAudio("/home/pi/discord/dasGlas/coin.mp3"))
await dc(vc)
# --------------------------------------------------------------------------------------------------------
def coin_plus(name, plus):
coin_config.read(coin_ini)
if coin_config.has_section(name):
count = coin_config.getint(name, 'count')
count = count + plus
coin_config.set(name, 'count', str(count))
else:
coin_config.add_section(name)
count = plus
coin_config.set(name, 'count', str(count))
with open(coin_ini, 'w') as coin_configfile:
coin_config.write(coin_configfile)
# --------------------------------------------------------------------------------------------------------
async def coin_stand_out(chanel):
coin_config.read(coin_ini)
stand = list()
await chanel.send('Der Stand ist:')
for x in coin_config.sections():
count = coin_config.getint(x, 'count')
stand.append([count, x])
stand.sort(reverse = True)
for item in stand:
if item[0] == 1:
await chanel.send(item[1] + ' hat eine Münze in das Glas geworfen')
else:
await chanel.send(item[1] + ' hat ' + str(item[0]) + ' Münzen in das Glas geworfen')
with open(coin_ini, 'w') as coin_configfile:
coin_config.write(coin_configfile)
# --------------------------------------------------------------------------------------------------------
async def play_kein_Pardon(name):
voicechannel = name.voice.channel
if voicechannel != None:
vc = await voicechannel.connect()
vc.play(discord.FFmpegPCMAudio("/home/pi/discord/dasGlas/kein_Pardon.mp3"))
await dc(vc)
# --------------------------------------------------------------------------------------------------------
def witz_plus(name, plus):
witz_config.read(witz_ini)
if witz_config.has_section(name):
count = witz_config.getint(name, 'count')
count = count + plus
witz_config.set(name, 'count', str(count))
else:
witz_config.add_section(name)
count = plus
witz_config.set(name, 'count', str(count))
with open(witz_ini, 'w') as witz_configfile:
witz_config.write(witz_configfile)
# --------------------------------------------------------------------------------------------------------
async def witz_stand_out(chanel):
witz_config.read(witz_ini)
stand = list()
await chanel.send('Der Stand ist:')
for x in witz_config.sections():
count = witz_config.getint(x, 'count')
stand.append([count, x])
stand.sort(reverse = True)
for item in stand:
if item[0] == 1:
await chanel.send(item[1] + ' hat einen schlechten Witz gemacht')
else:
await chanel.send(item[1] + ' hat ' + str(item[0]) + ' schlechte Witze gemacht')
with open(witz_ini, 'w') as witz_configfile:
witz_config.write(witz_configfile)
# Reaktion auf Nachrichten--------------------------------------------------------------------------------
# \U0001FA99 - coin
# \U0001F4B8 - money with wings
# \U0001F4B0 - moneybag
# \U0001f62d - sob
# \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:
if message.content.count('\U0001FA99') > 0:
if len(message.mentions) > 0:
await message.add_reaction('\U0001F4B8')
for j in message.mentions:
coin_plus(j.display_name, message.content.count('\U0001FA99'))
else:
await message.add_reaction('\U0001F4B0')
coin_plus(message.author.display_name, message.content.count('\U0001FA99'))
await play_coin(message.author)
elif message.content.startswith('!stand'):
await message.delete()
for x in message.author.roles:
if str(x) == "Meh":
await coin_stand_out(message.channel)
else:
await message.delete()
elif message.channel.name == ch_witz:
if message.content.count('\U0001f62d') > 0:
if len(message.mentions) > 0:
await message.add_reaction('\U0001f972')
for j in message.mentions:
witz_plus(j.display_name, message.content.count('\U0001f62d'))
else:
await message.add_reaction('\U0001f972')
witz_plus(message.author.display_name, message.content.count('\U0001f62d'))
await play_kein_Pardon(message.author)
elif message.content.startswith('!stand'):
await message.delete()
for x in message.author.roles:
if str(x) == "Meh":
await witz_stand_out(message.channel)
else:
await message.delete()
# 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)
# --------------------------------------------------------------------------------------------------------
@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 witz_stand_out(discord.utils.get(guild.channels, name = ch_witz))
await coin_stand_out(discord.utils.get(guild.channels, name = ch_coin))
# --------------------------------------------------------------------------------------------------------
client.run('ODE1Mzc0MDc0NDU5OTc5ODM2.YDreSA.q494wu3zvinCJ6QiffrWvoUVibc')
# --------------------------------------------------------------------------------------------------------