python - 如何在不使用不和谐机器人的情况下让机器人每小时在不和谐上发送消息?
问题描述
我想制作一个 python 机器人,它每小时向特定的不和谐频道发送一条消息,我在网上搜索过,但我只得到了使用不和谐机器人的教程。我想从我的 OWN 帐户而不是不和谐的机器人发送消息,我也是新手,所以我无法理解太复杂的代码!谢谢!
解决方案
这是一个基本的机器人,您可以使用它每小时发布一次;正如 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")
在#comments 中,这是非常不言自明的。
推荐阅读
- android - 如何更改统一 apk 的应用启动器图标
- android - Android Kotlin Retrofit Post Request 输入的数据未发送
- node.js - Angular - 从 API 中的 blob 绑定图像 url
- angular - 找不到模块“./product-list/product-list.component”
- php - 在 Laravel 7 中使用自定义身份验证时记住 Token 错误
- ios - 如何将模拟器上显示的 json 数据保存到本地存储(React-native)
- android-studio - 升级 gradle 到 v6.1.1 找不到方法
- python - Pymongo 查询以使用 Python 从 Mongodb 中的集合中获取最新文档
- audio-streaming - byte [] 传输使用 libjitsi
- python - 将年份添加到日期列中的日期