Home > Mobile >  Discord-Bot fails to message Users
Discord-Bot fails to message Users

Time:10-24

I'm currently working on a bot that, messages Users in a given json-file in certain time intervals. I tried this code but it doesn't do what it's supposed to nor does it give me an error I could work with.

@tasks.loop(seconds=10)
async def dm_loop():
    with open("users.json" "r") as file:
        users = json.load(file)
    for stgru in users:
        for i in users[stgru]:
            user = await client.fetch_user(i)
            await user.send("hello")

And just in case you're wondering: the short time Intervals and the unnecessary message: "hello", ist just for testing purposes. The "users.json"-file, mentioned in the code, has a format like this:

{
"724": ["name1#2819", "name2#2781", "name3#2891"],
"727": [],
"986": ["name4#0192"],
"840": ["name5#1221", "name6#6652"],
"798": ["name7#3312", "name8#8242", "name9#1153", "name10#3318"]
}

I already added the "dm_loop.start()"-method to my "on_ready()" but it's not working at all.

I'd be so glad if anyone could help me out here. Thanks

CodePudding user response:

According to the docs, fetch_user looks up users by their ID, so you need to store the user ID instead of the user name.

https://discordpy.readthedocs.io/en/master/ext/commands/api.html?highlight=fetch_user#discord.ext.commands.Bot.fetch_user

Otherwise you can create your own UserConverter. Here's an example on how you would do that.

from discord.ext import commands

[...]

user_name = "some_tag#1234"
user = await commands.converter.UserConverter().convert(ctx, argument=user_name)
await user.send("Hello")

I do really recommend the first option though, it is a lot simpler. Since you would have to create a custom context if you are not using this in a command i believe.

  • Related