【问题标题】:How do I make a bot to message every hour on discord without using a discord bot?如何在不使用不和谐机器人的情况下让机器人每小时在不和谐上发送消息?
【发布时间】:2021-12-15 01:58:49
【问题描述】:

我想制作一个 python 机器人,它每小时向特定的不和谐频道发送一条消息,我在网上搜索过,但我只得到了使用不和谐机器人的教程。我想从我的 OWN 帐户发送消息 而不是一个不和谐的机器人,我也是一个新手,所以我无法理解太复杂的代码! 谢谢!

【问题讨论】:

  • 您可以使用discord webhook,但它仍然不是您自己的帐户
  • Userbot(使用个人 discord 帐户以编程方式登录)违反 Discord TOS。我建议改用 webhook(如上所述)或机器人帐户。

标签: python discord bots


【解决方案1】:

这是一个基本的机器人,您可以使用它每小时发布一次;正如 Taku 所说,使用用户机器人会违反 Discord TOS。

import random
import discord
from discord.ext import tasks

# Init the discord client
discord_client = discord.Client()

# The channel ID you want the bot to send your message to (Right click channel -> Copy ID) 
channel_id = 000077777777770000

# Set the channel object using our channel ID number
channel = discord_client.get_channel(channel_id)

# List we will use these in the example to send random messages to the server
messages = [ "Hi I'm a bot!", "I'm just talking so you don't think I'm inactive!", "Blah blah blah  blah blah!", "Add however many you like me to say in here!" ]

# Single message to get sent to the server string
single_message = "This will send over and over if multi_message = False."

# We will use this boolean to determine if we are just sending message string or a random one from messages list
multi_message = False
        
# Create a loop task for every 60 minutes [1 hour]
@tasks.loop(minutes = 60)
async def send_message():
    # Call channel so we can modify it's value
    global channel
    # Make sure channel isn't null
    if channel == None:
        channel = discord_client.get_channel(channel_id)
    # Wait for the discord bot to load before we start posting
    await discord_client.wait_until_ready()
    # Check to see if we are going to be sending different messages every hour or not
    if multi_message:
        # Print to output
        print("Posted random message.")
        # Send message to Discord channel
        await channel.send(f"{random.choice(messages)}")
    else:
        print("Posted single message")
        await channel.send(f"{single_message}")

# On bot ready
@discord_client.event
async def on_ready():
    # Check to see if we are going to be sending different messages every hour or not
    if multi_message:
        print("* Sending random messages to the channel...")
    else:
        print("* Sending single message to the channel...")

    # Start the message sending loop
    send_message.start()

    # Finished setting up
    print("Bot ready.")

# Launch the Discord bot
print("+ Loading Discord message posting bot...")
discord_client.run("GET YOUR DISCORD TOKEN FROM DISCORD DEVELOPER PORTAL")

在#cmets 中,它非常不言自明。

【讨论】:

  • 即使知道被封号的风险,如果他想在自己的账户上运行这段代码,我推荐这篇关于如何获取账户令牌的教程:How to Find Your Discord Token。然后在代码中指定:discord_client.run("YOUR_TOKEN", bot=False)。为了更好地处理用户帐户的 API,我建议使用特定的自我机器人库,例如 discord.py-self
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-25
  • 2019-12-10
  • 2022-11-02
  • 2018-11-19
  • 2021-06-09
  • 2021-08-14
  • 2021-04-03
相关资源
最近更新 更多