Discord Bot mit Python

ActuallyRisk
26. Dezember 2022

Ich und ein guter Kollege (Marcel von Buddster.de) haben zusammen einen Discord Bot geschrieben mit umfangenden Features zum Feiern während der Corona Pandemie.

Folgende essenzielle Features sind vorhanden:

  • mehrere Mini Spiele mit Zufallsfaktor und ausschlussverfahren
  • Der Bot weiß, wer wann wie viel getrunken hat, und kann dementsprechend eine andere Auswahl beim Verteilen treffen
  • Erkennen von Chat und Emojis von Benutzern als Interkation mit dem Bot
  • Reaktion auf Befehle oder bestimmte Stichwörter im Chat

Das ganze Projekt wird von Zeit zu Zeit weiterentwickelt.

Code

Der folgende Code ist die zu startende bot.py.

#imports standard
import os
import random
import sched, time
import datetime

#imports discord
import discord
from discord import channel
from discord import reaction
from discord.ext import commands
import subprocess

#import settings
#import settings
from settings import TOKEN, teilnehmer, shots_id

gur = commands.Bot(command_prefix='!')

class Main(discord.Client):
    def __init__(self):
        super().__init__()


    @gur.command(name="info")
    async def info(ctx):
        embed = discord.Embed(
            title       = f"{ctx.guild.name}",
            description = "Corona Silvester Party (Online)",
            timestamp   = datetime.datetime.now(),
            color       = discord.Color.blue()
        )
        embed.add_field(name="Server created", value=f"{ctx.guild.created_at}")
        embed.add_field(name="Server owner", value=f"{ctx.guild.owner}")
        embed.add_field(name="Server Region", value=f"{ctx.guild.region}")
        embed.add_field(name="Server ID", value=f"{ctx.guild.id}")
        #embed.set_thumbnail(url=f"{ctx.guild.icon}")

        await ctx.send(embed=embed)

#GAME: SHOT ROULETTE

    @gur.command(name="shotroulette")
    async def shotroulette(ctx):
        #start new Python file in subprocess
        subprocess.Popen("python ./games/drunk.py")

#GAME: RANDOMSHOT

    @gur.command(name="randomshot")
    async def randomshot(ctx):

        #start new Python file in subprocess
        subprocess.Popen("python ./games/randomshot.py")

    @gur.command(name="clear")
    async def clear(ctx, amount=100):
        await ctx.channel.purge(limit=amount)


    

#gur.run(TOKEN)
client = Main()
gur.run(TOKEN)

    
    

"Drunk" (Spiel)

# discord_bot.py
import os
import random
import time
import discord

import settings
from settings import TOKEN, teilnehmer, shots_id

# create intents before creating bot/client instance
intents = discord.Intents().default()
intents.members = True
# create the instance
client = discord.Client(intents=intents)


@client.event
async def on_ready():

    channel = client.get_channel(787671119015837716)

    embed = discord.Embed(colour=discord.Colour(0xf50104), description="```\n❕  Klicke auf ✅ um teilzunehmen\n\n❗  Klicke auf ❌ wenn du nicht mit machen willst```")

    embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/787767111644086292/793563752669642802/alcohol.png")
    embed.set_author(name="SHOT ROULETTE", icon_url="https://cdn.discordapp.com/attachments/787767111644086292/793563754255482920/celebration.png")

    embed.add_field(name="➡️ TEILNEHMER:", value="-")
    embed.add_field(name="⏱️ ZEIT:", value="120")
    embed.add_field(name="⚠️ GEWINNER", value="-")
        

    moji = await channel.send(embed=embed, delete_after=300)


    print(moji.id)
    shots_id = moji.id
    await moji.add_reaction('✅')
    await moji.add_reaction('❌') 
    v_time=120
    while v_time:
        mins, secs = divmod(v_time, 60)
        timer = '{:02d}{:02d}'.format(mins, secs)
        print(timer, end="\r")
        time.sleep(1)
        v_time -= 1

        message = await channel.fetch_message(shots_id)

        embed = discord.Embed(colour=discord.Colour(0xf50104), description="```\n❕  Klicke auf ✅ um teilzunehmen\n\n❗  Klicke auf ❌ wenn du nicht mit machen willst```")
        embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/787767111644086292/793563752669642802/alcohol.png")
        embed.set_author(name="SHOT ROULETTE", icon_url="https://cdn.discordapp.com/attachments/787767111644086292/793563754255482920/celebration.png")
        
        if not teilnehmer:
            embed.add_field(name="➡️ TEILNEHMER:", value="-")
        else:
            embed.add_field(name="➡️ TEILNEHMER:", value=', '.join(teilnehmer))

        embed.add_field(name="⏱️ ZEIT:", value=str(v_time))
        embed.add_field(name="⚠️ GEWINNER", value="-")
        await message.edit(embed=embed)
  
   
    time.sleep(1)
    embed = discord.Embed(colour=discord.Colour(0xf50104), description="```\n❕  Klicke auf ✅ um teilzunehmen\n\n❗  Klicke auf ❌ wenn du nicht mit machen willst```")
    embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/787767111644086292/793563756550291496/drink.png")
    embed.set_author(name="SHOT ROULETTE", icon_url="https://cdn.discordapp.com/attachments/787767111644086292/793563754255482920/celebration.png")

    
    if not teilnehmer:
        embed.add_field(name="➡️ TEILNEHMER:", value="-")
    else:
        embed.add_field(name="➡️ TEILNEHMER:", value=', '.join(teilnehmer))

    embed.add_field(name="⏱️ ZEIT:", value="abgelaufen")
    
    if not teilnehmer:
        embed.add_field(name="⚠️ GEWINNER", value="-")
    else:
        embed.add_field(name="⚠️ GEWINNER", value=str(random.choice(teilnehmer)))

    await message.edit(embed=embed)

@client.event
async def on_reaction_add(reaction, user):
    channel = client.get_channel(787747036149055498)
    
    if reaction.message.channel.id != channel:
        pass
    if (reaction.emoji == '✅') and (user.name not in teilnehmer) and (user.name != 'Dingo'):
        teilnehmer.append(user.name)
    if (reaction.emoji == '❌') and (user.name in teilnehmer)and (user.name != 'Dingo'):
        teilnehmer.remove(user.name)
    print(teilnehmer)

client. Run(TOKEN)

"Randomshot" (Spiel)

# discord_bot.py
import os
import random
import time
import discord

import settings
from settings import TOKEN, teilnehmer, shots_id

# create intents before creating bot/client instance
intents = discord.Intents().default()
intents.members = True
# create the instance
client = discord.Client(intents=intents)


@client.event
async def on_ready():

    while True:

        channel = client.get_channel(787671119015837716)
        v_channel = client.get_channel(786964433124458560)

        v_teilnehmer = []
        #print(v_channel.members)
        for member in v_channel.members:
            v_teilnehmer.append(member.name)

        embed = discord.Embed(colour=discord.Colour(0xf50104), description="```❕ Es wird gleich ein Shot an jemanden verteilt ❕```")

        embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/787767111644086292/793563752669642802/alcohol.png")
        embed.set_author(name="SHOT ROULETTE", icon_url="https://cdn.discordapp.com/attachments/787767111644086292/793563754255482920/celebration.png")

        moji = await channel.send(embed=embed, delete_after= 360)

        time.sleep(30)

        print(moji.id)
        shots_id = moji.id

        v_time=60
        while v_time:
            mins, secs = divmod(v_time, 60)
            timer = '{:02d}{:02d}'.format(mins, secs)
            print(timer, end="\r")
            time.sleep(1)
            v_time -= 1

            message = await channel.fetch_message(shots_id)

            embed = discord.Embed(colour=discord.Colour(0xf50104), description="```❕ OH, JUNGE! Wen erwischt es? ❕```")
            embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/787767111644086292/793563752669642802/alcohol.png")
            embed.set_author(name="SHOT ROULETTE", icon_url="https://cdn.discordapp.com/attachments/787767111644086292/793563754255482920/celebration.png")
            embed.add_field(name="⏱️ ZEIT:", value=str(v_time))

            await message.edit(embed=embed)

        time.sleep(1)

        embed = discord.Embed(colour=discord.Colour(0xf50104), description="```❕ Lol, du musst trinken. Jetzt trink du Hund! ❕```")
        embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/787767111644086292/793563756550291496/drink.png")
        embed.set_author(name="SHOT ROULETTE", icon_url="https://cdn.discordapp.com/attachments/787767111644086292/793563754255482920/celebration.png")
        embed.add_field(name="⏱️ ZEIT:", value="abgelaufen")
        embed.add_field(name="❗ SPIELER:", value=str(random.choice(v_teilnehmer)))

        await message.edit(embed=embed)

        time.sleep(random.choice(600, 1800))
        

client. Run(TOKEN)

2022 - 2024 © ActuallyRisk.de
WordPress Cookie Hinweis von Real Cookie Banner