显然,discord 机器人可以具有移动状态,而不是默认获得的桌面(在线)状态。
经过一番挖掘后,我发现这种状态是通过修改 的值来实现的,IDENTIFY packet或者discord.gateway.DiscordWebSocket.identify理论上应该$browser让我们获得移动状态。Discord AndroidDiscord iOS
修改我在网上找到的执行此操作的代码片段后,我最终得到以下结果:
def get_mobile():
"""
The Gateway's IDENTIFY packet contains a properties field, containing $os, $browser and $device fields.
Discord uses that information to know when your phone client and only your phone client has connected to Discord,
from there they send the extended presence object.
The exact field that is checked is the $browser field. If it's set to Discord Android on desktop,
the mobile …Run Code Online (Sandbox Code Playgroud) 我有个问题。因此,每次用户在我的不和谐服务器上写入聊天消息时,我都会运行一个任务 - 它称为on_message. 所以我的机器人在这个事件中有很多事情要做,我经常遇到这种错误:
Task was destroyed but it is pending!\ntask: <Task pending name='pycord: on_message' coro=<Client._run_event() done, defined at /Bots/gift-bot/discord/client.py:374> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f68a7bdfc10>()]>>\nRun Code Online (Sandbox Code Playgroud)\n所以我想如果我想解决这个问题,我需要加速我的代码。但遗憾的是,我不知道如何修复此错误。
\n编辑:我集成了计时,这就是我打印的内容:
\nTask was destroyed but it is pending!\ntask: <Task pending name='pycord: on_message' coro=<Client._run_event() done, defined at /Bots/gift-bot/discord/client.py:374> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f01063f98e0>()]>>\n2 if checks done - 7.867813110351562e-06\n5 if checks done - 0.0061550140380859375\nmysql checks done - 0.010785341262817383\ntask done - 0.13075661659240723\n2 if checks done - 8.344650268554688e-06\n5 if …Run Code Online (Sandbox Code Playgroud) 我正在使用 Discord API 并注意到我无法访问消息的内容。
这是我的代码:
import discord
client = discord.Client()
@client.event
async def on_ready():
print(f'Logged in as {client.user}')
@client.event
async def on_message(message):
if 'My Name' in message.author.name:
print(f'Author: {message.author.name}')
print(f'Content: {message.content}')
print(f'Clean_Content: {message.clean_content}')
print(f'System_Content: {message.system_content}')
client.run(TOKEN, bot=False)
Run Code Online (Sandbox Code Playgroud)
请注意,由于显而易见的原因,令牌和我的用户名在本文中保持私密。
无论消息如何,这都是我得到的输出:
Author: My Name
Content:
Clean_Content:
System_Content:
Run Code Online (Sandbox Code Playgroud)
如您所见,我还尝试了 clean_content 和 system_content 属性。然而,它们都没有显示实际的消息。我还尝试过使用机器人帐户,结果令人惊讶地有效,但我希望它可以与我自己的帐户一起使用。问题是 Discord 不打算让私人客户阅读消息还是我错过了一些基本的东西?
情况:
我正在尝试使用 pycord 制作一个简单的不和谐机器人,但每次运行代码时都会出现以下错误:
Traceback (most recent call last):
File "main.py", line 3, in <module>
bot = discord.Bot()
AttributeError: module 'discord' has no attribute 'Bot'
Run Code Online (Sandbox Code Playgroud)
代码:
Traceback (most recent call last):
File "main.py", line 3, in <module>
bot = discord.Bot()
AttributeError: module 'discord' has no attribute 'Bot'
Run Code Online (Sandbox Code Playgroud)
我做了什么:
我已经检查过我是否安装了 pycord 以及我的令牌是否正确。
我有一个非常基本的脚本,它会弹出一条消息,其中包含带有命令 ?place 的按钮
单击此按钮后,机器人会向单击它的用户回复“嗨”。
如果按钮没有交互超过大约 3 分钟,它就会开始返回“交互失败”。
之后按钮就变得毫无用处。我假设有某种内部超时,我在文档中找不到。无论使用discord.py (2.0) 还是pycord,该按钮都会执行相同的操作。控制台没有任何反应。就好像按钮点击没有被拾取一样。
偶尔该按钮会再次开始工作,控制台上会出现大量以下错误:
discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction
Ignoring exception in view <View timeout=180.0 children=1> for item <Button style=<ButtonStyle.success: 3> url=None disabled=False label='click me' emoji=None row=None>:
Run Code Online (Sandbox Code Playgroud)
我认为超时 = 180 是此问题的原因,但有人知道如何停止此超时以及为什么会发生这种情况吗?我在文档中看不到任何关于不和谐按钮只能使用 3 分钟的内容。
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
bot = commands.Bot(command_prefix="?", intents=intents)
embed1=discord.Embed(title="Test", description = f"TESTING",color=0xffffff)
print("bot connected")
@ bot.command(name='place')
async def hello(ctx):
view = discord.ui.View()
buttonSign = discord.ui.Button(label …Run Code Online (Sandbox Code Playgroud) 最近我决定重写我的不和谐机器人并添加按钮。到目前为止我遇到的主要问题是,我无法在按下按钮后立即禁用按钮,人们告诉我,事实上button.disabled=True,它会禁用按钮,但它只是将其禁用,所以它永远不会被禁用按下。我想要的是能够单击它并执行它的操作,然后禁用它。
作为参考,我将放置一些代码
我使用 disnake,一个discord.py 分支,它确实具有与 dpy 相同的语法,但我们有按钮和斜杠命令、下拉菜单等
class BlurpleButton(Button):
def __init__(self, label, emoji=None):
super().__init__(label=label, style=discord.ButtonStyle.blurple, emoji=emoji)
Run Code Online (Sandbox Code Playgroud)
这是为了更容易使用按钮,我创建了一个模板,我可以在任何命令上使用它
class CustomView(View):
def __init__(self, member: disnake.Member):
self.member = member
super().__init__(timeout=180)
async def interaction_check(self, inter: disnake.MessageInteraction) -> bool:
if inter.author != self.member:
await inter.response.send_message(content="You don't have permission to press this button.", ephemeral=True)
return False
return True
Run Code Online (Sandbox Code Playgroud)
这是为了按钮只能由提到的成员按下,例如,如果我这样做/test @member(由于不和谐新的特权意图,我迁移到斜杠命令),那么只有该成员才能按下它,而其他人则无法按下。
到目前为止,一切正常,现在我们在命令中“组装”它之后
@commands.slash_command(description='test')
async def test(self, inter):
(do stuff in there)
. . .
button1 = BlurpleButton("Button name")
view=CustomView(member)
view.add_item(button1)
async …Run Code Online (Sandbox Code Playgroud) 我正在使用 pycord 制作一组简单的斜杠命令来实现不和谐。
import discord
bot = discord.Bot()
testingServer = [{server ID}]
@bot.slash_command(guild_ids = testingServer, name ="verify_help", description="blabla" )
async def verifyHelp(ctx):
embed=discord.Embed(title="Verify Your Wallet", description = "help goes here",color=0xffffff)
await ctx.respond(embed = embed, ephemeral=True)
bot.run({TOKEN})
Run Code Online (Sandbox Code Playgroud)
我相信可以创建多字斜线命令,如不和谐 API 文档中所示:
即使用斜杠命令作为 /verify help 而不是 /verify-help
https://discord.com/developers/docs/interactions/application-commands
我相信我需要将“选项”部分翻译成 pycord 但不知道语法。它建议并列出选项,因此 options = []。这就是我被困住的地方。
lashcommand 的 pycord 手册在这里:https ://docs.pycord.dev/en/master/api.html#slashcommand
我正在尝试加载齿轮(位于cogs目录中),但每次尝试加载它时都会收到以下错误消息:
discord.errors.ExtensionFailed: Extension 'cogs.cmds' raised an error: AttributeError: 'Bot' object has no attribute 'add_command'
Run Code Online (Sandbox Code Playgroud)
我完全按照教程所示进行操作,但仍然不起作用。这是我的代码:
discord.errors.ExtensionFailed: Extension 'cogs.cmds' raised an error: AttributeError: 'Bot' object has no attribute 'add_command'
Run Code Online (Sandbox Code Playgroud)
齿轮文件:
# main.py
import discord, os, dotenv
# setup
bot = discord.Bot(
intents = discord.Intents.default(),
prefix = "zrun "
)
@bot.event
async def on_ready():
print(f'Connected as {bot.user}')
# load cmds
bot.load_extension('cogs.cmds')
# get token and run bot
dotenv.load_dotenv()
bot.run(os.getenv('TOKEN'))
Run Code Online (Sandbox Code Playgroud)
是的,我搜索过 StackOveflow,但没有与齿轮相关的答案解决我的问题。
我的 Pycord 机器人中有一个斜杠命令。这是代码:
@bot.slash_command(name='greet', description='Greet someone!')
async def greet(ctx, name):
await ctx.send('Hello ' + name + '!')
Run Code Online (Sandbox Code Playgroud)
我如何使“名称”成为可选参数?我尝试设置 name=None,但它不起作用。
大家好,2022 年新年快乐!
自从最近为不和谐添加了“超时”以来,我尝试按照一些教程制作超时命令,例如:
https://docs.pycord.dev/en/master/api.html?highlight=timeout#discord.Member.timeout
但由于我不知道的原因,我可能会收到以下错误:
Ignoring exception in command timeout2:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "/home/runner/Russia-Bot/moderator.py", line 42, in timeout2
await member.timeout_for(time)
AttributeError: 'Member' object has no attribute 'timeout_for'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in …Run Code Online (Sandbox Code Playgroud) 我正在从 Novus 切换到 Pycord 并尝试转换按钮功能。我正在尝试获取一个列表并添加按钮 1-n 来表示列表的长度,然后获取所选按钮的 custom_id 。选择后,我想继续该命令并在命令的其余部分中使用 custom_id。到目前为止,这就是我所拥有的:
@bot.slash_command(name='bombs', description='Returns bombs to destroy base and airfield.')
async def bomb(ctx):
await ctx.interaction.response.defer()
countries = ["America", "Britain", "China", "France", "Germany", "Italy", "Japan", "Russia", "Sweden"]
view = DefaultView()
for number in list(range(1, len(countries) + 1)):
view.add_item(DefaultButton(label=str(number), custom_id=str(number)))
country_choice_message = await ctx.interaction.followup.send("Select a country to view bombs from:", view=view)
timed_out_ = await view.wait()
if timed_out_:
view.disable_all_items()
view.stop()
await country_choice_message.edit(view=view)
await ctx.interaction.followup.send("Timed out.", ephemeral=True)
return
# country_number = button_custom_id
# do …Run Code Online (Sandbox Code Playgroud) pycord ×11
python ×10
discord ×7
discord.py ×6
python-3.x ×3
timeout ×2
disnake ×1
mysql ×1
slash ×1
task ×1