I've been having an issue recently with getter methods from different objects in discordpy, in this example I'm using discord.Guild
guild = await bot.fetch_guild(key) # API call used because getter returns nothing
categories = guild.categories # returns empty list
Intents are all enabled, I used discord.Intents.all().
Privileged gateway intents are also all enabled in Developer Portal.
Can someone help me with this issue? Thanks.
Related
I'm working on a basic bot with discord.py and I need to figure out how to send a DM when a new member joins. When a new user joins, it should trigger an event which will get the member and send them a DM welcoming them to the server. I've tried quite a few variations of code trying to get it to work but nothing happens when I join with an alt.
Here's the basic code I have:
async def on_member_join(member):
await member.create_dm()
await member.send(
f'Hi {member.name}! Welcome to the server!'
)
I've also tried without the member.create_dm() because the docs said that it should do that on its own. I'm using Python 3.7.3 and discord.py 1.5.1 so old answers from a couple years ago don't work. I've tried everything that I can find.
You need to enable intents.members, also I saw a handful of questions similar like these so you didn't do a good job when researching.
intents = discord.Intents.default()
intents.members = True
# If you're using commands.Bot
bot = commands.Bot(..., intents=intents)
# If you're using discord.Client
client = discord.Client(intents=intents)
Also make sure to enable them in the developer portal
Starting with discord.py 1.5, in order to receive the member join event, you must enable the members gateway intent, which you can read about here. You must first enable to members intent on the developer portal, then when constructing your client/bot instance, you must pass your intents to it.
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(**other_options, intents=intents)
# Or if using the clinet
client = discord.Client(**other_options, intents=intents)
Finally, you should also be sure to handle the Forbidden exception that may be raised if the member has their DMs turned off
try:
await member.send('Some text')
except discord.Forbidden:
# The user had their DMs disabled, do whatever you need to
# nothing in this case
pass
I'm currently programming a bot and my first goal is to make the bot welcome and say goodbye to members that join or leave, but the bot sends nothing, not even showing any error.
#client.event
async def on_member_join(member):
print(" System: A member has joined the server!")
def sprint(str):
for c in str + '\n':
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(3./90)
sprint(" Bot: Oh hey, looks like "+member.name+" has joined..")
#send message to a specific channel
channel = client.get_channel(channel id from the welcome channel)
await channel.send("Hey! Welcome to the server, "+member.mention+"! I hope you read the #rules before doing anything, otherwise have fun and enjoy your stay!")
I did it last time before I recreated the script, but the on_member_join thing doesn't work, not even on_member_leave! (I'm doing the on_member_join first to check if it's working)
My alternate account rejoined my server, but the bot (except MEE6) didn't send anything.. Is there anything you guys can help me?
You'll need to enable the member intent by creating an instance of discord.Intents.default, setting Intents.members to True, and passing the instance into your commands.Bot or discord.Client constructor, like:
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(intents=intents)
You'll also need to enable the members intent in the Discord dev portal (https://discord.com/developers). Go to that url, click on your application, go to the Bot tab, scroll down to the 'privileged gateway intents' section, and enable the members intent.
Then, restart your bot, and member events should work.
(for more info, look at https://discordpy.readthedocs.io/en/latest/intents.html)
We have a calling enabled teams bot, that receives calls. From the data it receives, we want to retrieve who is actually calling. For PSTN calls, this is ok. However, for Teams-to-Teams-calls (or SfB-Teams calls) we get the calling identity as (underneath example is SfB->Teams):
"identity":{
"#odata.type":"#microsoft.graph.identitySet",
"onPremises":{
"#odata.type":"#microsoft.graph.identity",
"id":"<guid>",
"tenantId":"<guid>",
"identityProvider":"None"
}
When all participants live in the tenant of the calling bot, we can retrieve all info (by using the graph API and just query the user with that objectID). But in the case the caller is from another tenant, how can we retrieve the displayname/sipadres for the identity?
For example, if I am being called, my teams client shows a display name. Now if the bot is called, how can it know the displayname (or sip address)?
I've tried to read out more properties using https://graph.microsoft.com/beta/communications/calls/..., however, that returns a 'call not found' consistently.
Specifically we don't support calls from sfb with details of caller. From Teams client should be able to get caller object ID though
I am trying to make it so server moderators are able to temporarily mute users in their Discord. I am not an experienced developer within Discord bots but learning.
What I am trying to do?
I am trying to make it so server owners are able to temp mute users in their Discord servers.
This is what I currently have for my Mute command:
#bot.command(pass_context=True)
async def mute(ctx, user: discord.Member):
if ctx.message.author.server_permissions.kick_members:
role = discord.utils.get(user.server.roles, name="Muted")
embed = discord.Embed(title="{} has been muted!".format(user.name), description="When the user needs unmuting do !unmute #user!" , color=0x0072ff)
embed.set_footer(text="Reversed by Damian#9209 | Reduction#9975")
embed.set_thumbnail(url=user.avatar_url)
await bot.add_roles(user, role)
await bot.say(embed=embed)
else:
embed = discord.Embed(title="Permission Denied.", description="You don't have permission to use this command.", color=0xff0000)
embed.set_footer(text="Reversed by Damian#9209 | Reduction#9975")
await bot.say(embed=embed)
As I am sure you are aware, the Discord.py library has changed to no longer include the rewrite branch. This means that the mute command has been made way easier for you. I'll provide some code to get you started. I will also provide links to all the documentation to each line.
#bot.command(name="tempmute",description="Temporarily mute a member")
#commands.has_permission(mute_members=True)
async def _tempmute(ctx,user:discord.Member):
muteRole = discord.utils.get(ctx.guild.roles,name="Muted")
await user.add_roles(muteRole)
has_permission is a check for Discord user permissions
utils.get is a utility function that can step through iterables and find the correct search filter. Here, we are using it to find a role with the name "Muted"
add_roles is a coroutine function (aka needs to be awaited) that adds one or multiple roles to a user.
I suggest also making sure that the role has the speak permission disabled so that you don't have to deal with it through the on_message event. Hope that helps!
I am developing a Google Actions project for Google Home using api.ai.
Is it possible to pass a state parameter as part of the response to api.ai and have it included in the next request?
The Amazon Alexa API handles such a flow and it came in handy.
Thanks
EDIT:
I did not mention this before: I have api.ai sending requests to a webhook after google assistant triggers my intents. The response api.ai expects is defined here. I've tried including extra fields in the response but these are not included in any future intent requests from api.ai. I've also tried adding fields and values to the google specific portion of the response (defined here) to no avail.
As an example of what I am interested in, when responding to Alexa requests we can include a json field "sessionAttributes" which are then passed by Amazon in any future requests that are part of that interaction.
In this instance I query a database key on the first intent (which is subsequently used to pull a record) and pass that key in sessionAttributes to avoid performing that lookup for every intent request I receive.
The equivalent that you are looking for to sessionAttributes in Alexa dev depends on whether you are using an API.ai webhook or a conversation webhook. Reference this doc for more info on the differences.
Since you are using API.ai, assuming you are using the Node.js client library, use the snippet below. 'Contexts' can store data, not just serve as a flag of sorts to establish where you are in a conversation. Call setContext just before you call 'ask' and close out your webhook fulfillment.
app.setContext('<context name>', <lifespan of context>, <JSON to store>)
Then for the next round of fulfillment, retrieve the JSON from the parameters object within the Context. Get it with:
var myContext = app.getContext('<context name>')
Reference the examples in these docs for more info.
You could create an optional parameter in API.ai to catch/store this value and append any message being sent to API.ai with a marker and then the database value you want to cache, so API.ai recognizes from the marker the value to be cached as the parameter, and this would be passed back out of API.ai as well, so if you needed to chain/loop this, on your side you again just check if it has the special 'append' parameter value to append that to the next user message.
That being said, contexts can likely achieve the same end-goal you are trying to achieve in a much more straightforward fashion