【问题标题】:Discord Poll Bot不和谐投票机器人
【发布时间】:2023-03-02 22:22:01
【问题描述】:

嗨,我正在尝试制作一个投票机器人,但我遇到了一个问题,这里的代码忽略了 + poll 以外的其他命令

import discord
import os
import requests
import json
import random

pollid = 0
emoji1 = '\N{THUMBS UP SIGN}'
emoji2 = '\N{THUMBS DOWN SIGN}'
client = discord.Client()

sad_words=["sad","depressed", "unhappy","angry","miserable","depressing"]

starter_encouragements = ["Cheer up", "hang in there.", "You are a great person / bot!"]

def get_quote():
  response = requests.get("https://zenquotes.io/api/random")

  json_data = json.loads(response.text)

  quote = json_data[0]['q'] + " -" + json_data[0]['a']
  return[quote]


from discord.utils import get

@client.event
async def on_ready():
  print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
  global pollid
  #if message.author == client.user:
   # return
  msg = message.content

  if message.content.startswith('+encourage'):
    quote=get_quote()
    await message.channel.send(quote)

  if any(word in msg for word in sad_words):
    await message.channel.send(random.choice(starter_encouragements))

  if message.content.startswith("+joke"):
    from dadjokes import Dadjoke
    dadjoke = Dadjoke()
    await message.channel.send(dadjoke.joke)
  if message.content.startswith("+poll"):
    pollid = pollid+1
    await message.channel.send(content = msg.split(' ', 1)[1] + ' (number of polls made ' + str(pollid) + ')')
     
  if message.author == client.user:
    await message.add_reaction(emoji1)
    await message.add_reaction(emoji2)

  reaction = get(message.reactions, emoji=emoji1) 
    #reaction2 = get(message.reactions, emoji=emoji2)
    #if (reaction != None and reaction2 != None):
     # totalcount = reaction.count + reaction2.count
    #if totalcount>=2:
  if (reaction != None and reaction.count != 1):
    await message.channel.send('The outcome of the poll is yes'+ str(reaction.count))
     
#  await message.channel.send('The outcome of the poll is no')
    

client.run(os.getenv('TOKEN'))    






我对 python 和不和谐 api 非常陌生,我一直在尝试建立一个投票系统,在该系统中,每次投票都有一个计时器,持续 24 小时,24 小时后它会将消息的反应量与看哪一方获胜。有人可以帮我弄这个吗。谢谢

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    我不会为此使用 on_message 事件,而是使用命令。 你可以这样做:

    import discord
    from discord.ext import commands
     
    @client.command()
    async def poll(ctx, *, text: str):
        poll = await ctx.send(f"{text}")
        await poll.add_reaction("✅")
        await poll.add_reaction("❌")
    

    这里我们将text 用作str,因此您可以添加任意数量的文本。

    如果你想在 24 小时后比较它,你还必须在机器人重新启动时建立一个冷却保护程序,否则我们可以使用 await asyncio.sleep(TimeAmount)

    如果您想使用命令检查结果,我们可以这样做:

    from discord.utils import get
    import discord
    from discord.ext import commands
    
    @client.command()
    async def results(ctx, channel: discord.TextChannel, msgID: int):
        msg = await channel.fetch_message(msgID)
        reaction = get(msg.reactions, emoji='✅')
        count1 = reaction.count # Count the one reaction
        reaction2 = get(msg.reactions, emoji="❌")
        count2 = reaction2.count # Count the second reaction
        await ctx.send(f"✅**: {count1 - 1}** and ❌**: {count2 - 1}**") # -1 is used to exclude the bot
    

    命令的用法是:results #channel MSGID

    请注意,fetch 是一个 API 调用,可能会导致速率限制。

    【讨论】:

      猜你喜欢
      • 2020-12-04
      • 2018-03-20
      • 2020-07-03
      • 2020-09-23
      • 2021-10-30
      • 2021-08-05
      • 2021-05-27
      • 2021-07-15
      相关资源
      最近更新 更多