如何使用不和谐机器人禁止随机玩家不和谐?

时间:2021-03-20 17:49:09

标签: python list random discord discord.py

我正在我的 Discord 机器人上制作俄罗斯轮盘赌功能。 你输入一个玩家列表,它会禁止其中一个。

这是我写的一些伪代码:

elif message.content.startswith("!Russian"):
    l = []
    l.append(input("Enter a name"))
    l.append(input("Enter a name"))
    l.append(input("Enter a name"))
    l.append(input("Enter a name"))
    l.append(input("Enter a name"))
    l.append(input("Enter a name"))
    playertoban = l[random.randint(1, 6)]
    
    ban(playertoban) #I wanna make a function that bans playertoban for one day

注意:它应该只禁止一名玩家一天。

这是完整的代码

import discord
import os
import random

from discord.ext import commands
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

bot = commands.Bot(command_prefix='!')
client = discord.Client()

@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

@client.event
async def on_message(message):
  if message.content.startswith("!calculate"):
    var = message.content
    var = var.replace("!calculate", "")
    var = var.replace(" ", "")
    var = var.replace("^", "**")
    var = var.replace("x", "*")
    var = var.replace("X", "*")
    try:
      result = eval(var)
      await message.channel.send(str(result))
    except:
       await message.channel.send("That is not a valid equation")
  elif message.content.startswith("!spam"):
    if "plane" in message.content:
      c = -1
      for i in range(100):
        stre = ""
        c = c + 1
        for h in range(c):
          stre = stre + "-"
        stre = stre + ":airplane_departure:"

        await message.channel.send(stre)    
    elif "boat" in message.content:
      c = -1
      for i in range(100):
        stre = ""
        c = c + 1
        for h in range(c):
          stre = stre + "-"
        stre = stre + ":man_rowing_boat:"

        await message.channel.send(stre)   
  elif message.content.startswith("!Er") or message.content.startswith("!Har") or message.content.startswith("!Vil") or message.content.startswith("!Hadde") or message.content.startswith("!Skal") or message.content.startswith("!Kommer") or message.content.startswith("!Tenker"):
    re = random.randint(1, 10)
    if re == 1:
      await message.channel.send("Ja")
    elif re == 2: 
      await message.channel.send("Nei")
    elif re == 3:
      await message.channel.send("Kanskje")
    elif re == 4:
      await message.channel.send("Vanskelig å si")
    elif re == 5:
      await message.channel.send("Definetivt!")
    elif re == 6:
      await message.channel.send("Sannsynligvis")
    elif re == 7:
      await message.channel.send("Antagelig ikke...")
    elif re == 8:
      await message.channel.send("Heck no!")
    elif re == 9:
      await message.channel.send("Hell ye!")
    elif re == 10:
      await message.channel.send("Vet ikke, men skal tenke litt på det")
  


我需要关于禁止部分的帮助,我已经知道选择谁部分。

2 个答案:

答案 0 :(得分:3)

您不应该将 on_message 用于命令。

获得玩家名称后,您可能希望获得 Member 对象。

然后你会想要禁止它们,删除消息 0 天。记录1天时间的日期时间。然后将它们添加到数据库/json 文件中。然后使用 Tasks 检查它是否已达到该时间,然后取消禁止它们,然后将它们从数据库/json 文件中删除。

答案 1 :(得分:1)

就像 FluxedScript 所说的,您可能想要使用 discord.py command 而不是 on_message。如果你这样做,你可以试试这个:

import random

from discord.ext import commands
from discord.ext import tasks


class DiscordBot(commands.Bot):
    
    def __init__(self):
        intents = discord.intents.default()
        intents.members = True
        self.bot = commands.Bot(prefix="!", intents=intents)

        self.bot.add_cog(RussianRoulette(self.bot))


class RussianRoulette(commands.Cog):

    def __init__(self, bot):
        self.bot = bot
        self.banned = {}

    @commands.command(name="russian", *names)
    async def russian(self, ctx):
        members_list = [discord.utils.get(ctx.guild.members, name=n) for n in names]
        member = random.choice(member_list)
        member.ban(reasion="Russian Roulette")
        self.banned.setdefault(
            member,
            datetime.datetime.now()+datetime.timedelta(days=1)
        )

    @tasks.loop(minutes=1.0)
    async def check_unban(self):
        for mem, dtime in self.banned.items():
            if datetime.datetime.now() <= dtime:
                mem.unban(reason="Ban expired")


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    bot = DiscordBot()
    loop.add_task(bot.start("TOKEN"))
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass

现在假设您的机器人实例已正确设置。您还需要激活一些gateway intents