不和谐投票机器人 [英] Discord Poll Bot

查看:39
本文介绍了不和谐投票机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个轮询机器人,但是我在这里遇到了一个问题,就是代码忽略了+ poll之外的其他命令

Hi i am trying to make a poll bot but ive encountered a problem here is the code ignore the other commands other than + 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和它的discord api非常陌生,它一直试图建立一个轮询系统,在该系统上,每次轮询都有一个计时器,该计时器持续24小时,并在24小时后将对消息的反应量与看看哪一方胜出.有人可以帮我弄这个吗.谢谢

i am very new to python and the discord api for it ive been trying to set up a poll system where it has a timer on each poll that lasts 24 hrs and after 24 hrs it compares the amount of reactions on the message to see which side wins. Can someone help me with this. Thanks

推荐答案

我不会为此使用 on_message 事件,而是使用命令.您可以执行以下操作:

I would not use on_message events for that but instead use a command. You can do something like this:

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 ,因此您可以根据需要添加任意数量的文本.

Here we have text as a str so you can add as many text as you want.

如果要在24小时后进行比较,则如果机器人重新启动,还必须内置一个冷却时间保护程序,否则我们可以使用 await asyncio.sleep(TimeAmount)

If you want to compare it after 24 hours you would also have to build in a cooldown saver if the bot restarts, otherwise we could go with await asyncio.sleep(TimeAmount)

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

If you want to check the result with a command we could go for this:

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

该命令的用法为:结果#channel MSGID .

请注意,提取是API调用,可能会导致速率限制.

Be aware that fetch is an API call and could cause a ratelimit.

这篇关于不和谐投票机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆