- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不熟悉 Discord 机器人或 Python 的大部分内容,所以这是一个我无法找出答案的简单问题。
我有两个文件; Discord_bot.py 和 test.py如何从 test.py 转发消息以将其发送到 Discord 中的 channel ?
测试.py
import discord_bot
discord_bot.signal(msg = "Hi")
discord_bot.py
import discord
from discord.ext import commands
TOKEN = '1234567890'
bot = commands.Bot(command_prefix='!')
@bot.command()
async def signal(ctx, *, msg):
await ctx.send(msg)
Discord 机器人工作正常,但从测试中调用信号函数并不是正确的方法。请问这里有什么帮助吗?
最佳答案
需要解开的内容有很多。
<强>0。切勿在线发布您的 Discord token 。如果您的 token 在线发布,Discord 可能会自动使其失效。
<强>1。您目前尚未运行机器人,请在末尾添加 bot.run(TOKEN)
<强>2。 Discord 机器人扩展的命令如何工作
@bot.command()
是 decorator ,如果您不知道它们是如何工作的,请阅读它。过于简化,它们会获取您的函数并在机器人中注册。
commands
扩展的内部工作原理基本上是:
signal(ctx, *args)
这就是 ctx 对象不能定位的原因,因为该函数在机器人内部工作中作为普通参数调用的方式。
<强>4。不要尝试创建自己的上下文对象,除非您知道自己在做什么。只有在覆盖默认消息解析器时才需要创建上下文对象。
<强>5。不要为此使用命令。
据我所知,你想做什么:自己调用命令。这很简单:
文件 1:
@bot.command()
async def signal(ctx, *, msg):
print(msg)
文件2:
from file1 import signal
import asyncio # if you don't know asyncio, read up on it
asyncio.run(signal(None, 'This is an argument'))
这很容易工作,它会打印你的东西。但您不希望将其打印出来,对吧?您希望它在 channel 中发送。这就是你需要上下文对象的原因,我之前说过,不要自己构建。那么我们实际上如何做到这一点呢?
答案是:不要使用命令。它们用于对消息使用react,而不是由它们自己调用。
<强>6。您(可能)想要的解决方案
所以这里的主要变化是:
信号现在是一个没有装饰器的普通异步函数
我们实际上指定了一个 channel ,我们希望将内容作为函数的参数发送到该 channel
文件 1:
import discord
from discord.ext import commands
TOKEN = 'do not share your token online'
bot = commands.Bot(command_prefix='!')
# as the channel_id, pass the channel_id you want the message to be sent in
async def signal(msg, channel_id):
global bot # getting our bot variable from the global context
channel = bot.get_channel(channel_id)
await channel.send(msg)
bot.run(TOKEN)
这里的主要变化是:
文件2
from file1 import signal
from time import sleep
import asyncio
sleep(5) # We need to give our bot time to log in, or it won't work
asyncio.run(signal('hi!', 123))
关于python - 从另一个文件调用函数 - Discord bot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60254002/
当我在 repl.it 上运行我的 discord 机器人时,出现错误。在错误中,我在下面看到了一些 HTML 代码。 CloudFlare 似乎阻止了我的机器人访问 Discord。这是什么原因造成
我计划使用 python 制作一个不和谐的机器人。 discord.py 和 discord.py[voice] 有什么区别? 最佳答案 discord.py 和 discord[voice] 之间的
我正在尝试编写一个脚本来检测用户是否以及何时将他们在 discord 上的名称从“bob”更改为“.bob”之类的名称,它会禁止此更改并将其保留为“bob”。 if (user.nickname.st
我想使用 Discord Bot 向用户发送私有(private)消息。 用户与机器人不在同一台服务器上。 如果我可以使用 author.sendMessage,我该如何初始化(查找)作者变量? 我可
我正在尝试将某人添加到特定服务器,然后通过 DM 只用不和谐 ID 表示该人。 它的工作方式是,有人在网站上使用 Discord OAuth2 登录自己,登录后他应该被添加到特定服务器,然后机器人应该
我无法弄清楚如何使用类似于 /giphy、/xivdb 等的实时“反馈”来实现斜杠命令。没有公共(public) API 吗? ?我正在使用 discord.py,但我想这对于这个问题来说并不重要。
我正在尝试使用 Discord Bot 在 discord 服务器上创建一个类别,但我无法在 Internet 上找到该方法或其他内容。我还看了“discord.js.org”。然后我认为没有任何可能
我正在为桌面创建灯,当我的麦克风在 Discord 上处于事件状态时,该灯将为绿色,当我的麦克风静音时,该灯将为红色。所以我想从不和谐中获取我的麦克风状态。但我不知道怎么做。 当我不在进行语音聊天时,
我已经看到了您允许某个角色使用命令的变体。但是,我试图实现完全相反的效果:如何禁止某个角色使用命令。 我搜索了堆栈溢出,没有找到答案,也没有在 discord.py 官方文档上找到答案。任何形式的帮助
我开发了自己的简单 discord bot(使用 discord.js)并将其部署到节点服务器。 一切正常。 现在我想为它添加更多功能。在开发过程中,我想在本地进行测试(当然)。 问题是:我可以在本地
我正在使用 Discord.js 创建一个基本的 Discord 机器人。当机器人第一次启动时,我运行 client.guilds.array()获取机器人当前订阅的所有公会的列表。我将其保存到其他程
我想为付费成员(member)提供 Discord 服务器。我想我可以让他们通过 Stripe 付款,向他们发送付款邀请,然后定期运行 Python 脚本,该脚本检查我的所有 Discord 成员是否
我试图向我的机器人添加一些新功能,但它最终没有工作,所以我删除了新代码,现在它不会响应任何命令。它仍然在线,但正如我之前所说,它完全没有响应。我已经查看了代码,但找不到问题。我对编码机器人还是个新手,
我正在编写一个建议机器人,它应该将玩家的建议发送到我服务器中的建议 channel ,并在建议 channel 中用一些表情符号使用react。 问题是使用“消息”作为消息参数会对发送到触发代码的消息
我想创建一个清除命令,如果 example .clear @user#0000 100 它将清除@user#0000 发送的最后 100 条消息。这是我的代码: @commands.comma
所以,我想为我的机器人创建一个命令,它可以从给定的消息 ID 中获取所有反应并将这三个类别存储到数组中,对于每个 react : react 的名称(例如:smile:) 使用react的用户(例如
我正在尝试在 discord.js 上制作一个简单的右舷系统,删除消息部分让我很困惑。目前,如果一条消息获得星号,则会在右舷 channel 创建一个嵌入,而且至关重要的是,页脚是原始消息的 ID。我
我最近启动了一个新的 discord.js 机器人,但出现“无效 token ”错误。这不可能是因为 async 功能,因为我只启动了机器人并且我有 0 个命令,上次发生这种情况是当我试图在 hero
我正在尝试向我的机器人添加一条命令,该命令会回复用户邀请到服务器的总人数 我的代码: if message.content.startswith('!invites'): totalInvit
我在一些服务器/公会中添加了一个 Discord 机器人。我可以使用此处提供的资源获得特定公会: https://discord.com/developers/docs/resources/guild
我是一名优秀的程序员,十分优秀!