Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有办法让我的discord bot在不同的服务器上发送相同的消息(而不跳过旧的消息)_Python_Discord_Discord.py - Fatal编程技术网

Python 有没有办法让我的discord bot在不同的服务器上发送相同的消息(而不跳过旧的消息)

Python 有没有办法让我的discord bot在不同的服务器上发送相同的消息(而不跳过旧的消息),python,discord,discord.py,Python,Discord,Discord.py,很抱歉,我不能正确地提出这个问题。当前,当有人在一台服务器上使用我的“meme”命令时,它会显示meme。但是,如果另一台服务器上有人使用相同的命令,我的bot会跳过它已经显示的meme。我是编程新手,有人能告诉我如何解决问题并解释他们做了什么吗?谢谢 @client.command() async def meme(ctx, typememe="hot"): if typememe == "hot": y = next(hot_memes

很抱歉,我不能正确地提出这个问题。当前,当有人在一台服务器上使用我的“meme”命令时,它会显示meme。但是,如果另一台服务器上有人使用相同的命令,我的bot会跳过它已经显示的meme。我是编程新手,有人能告诉我如何解决问题并解释他们做了什么吗?谢谢

@client.command()
async def meme(ctx, typememe="hot"):
    if typememe == "hot":
    y = next(hot_memes)
    x = y.url
    z = y.title
    votes = y.score
    comments = y.num_comments
    embed = discord.Embed(
        colour=discord.Colour.blue(),
        title=z,
        url=x
    )

    embed.set_image(
        url=x)
    embed.set_footer(
        text=f"Upvotes:{votes}"
    )
    await ctx.send(embed=embed)
elif typememe == "controversial":
    y = next(controversial_memes)
    x = y.url
    z = y.title
    votes = y.score
    comments = y.num_comments
    embed = discord.Embed(
        colour=discord.Colour.blue(),
        title=z,
        url=x
    )

    embed.set_image(
        url=x)
    embed.set_footer(
        text=f"Upvotes:{votes}"
    )
    await ctx.send(embed=embed)

我要做的是有一个字典来存储公会id和meme命令被调用的时间,比如
{guild_id,4}
其中4是“meme”被调用的时间,我们称之为memestorage。之后,每次调用meme命令时,您都可以使用
memestorage.keys()
检查memestorage的密钥中是否有meme命令,如果没有,则使用
memestorage[ctx.message.guild]=0添加memestorage命令。最后,在memestorage中ctx.message.guild的整数中添加一个,并在该索引+1处获取meme。所有这些加在一起可能看起来像:

memestorage={}
memeiter=[meme1,meme2,meme3,meme4]
@client.command()
异步def内存(ctx):
guild=ctx.message.guild
如果不在memestorage.keys()中:
memestorage[公会]=0
memestorage[公会]+=1
如果memestorage[guild]>=len(memeiter):
memestorage[公会]=1
pickedmeme=memeiter[memestorage[guild]+1]

这样,您还可以将memestorage dict保存在一个json文件中,这样即使在bot脱机时也可以存储数据。

您的
hot\u memes
是一个itertools.cycle对象,调用
next
,使其移动到周期中的下一项。所以你的
hot\u memes
的行为就像一个循环1->2->…n->1。您必须使用不同的逻辑来确保每次运行命令时(至少在大多数情况下)都会得到一个“随机”。哦。有没有办法,我可以改变这种行为??你能提出一些其他的逻辑吗?我在发帖前试过了,但一开始没用!我是个傻瓜,竟然在函数中包含memestorage dict!谢谢你的帮助